I'm working on a small project where a customer can be the master in a master-detail relationship to more than a single project object. Projects have a currency 'Max_Budget__c' and what I want to do is calculate the max budget per customer by adding the Max_Budget__c per project. This means that I need to collect a List per customer but also need customer stored so I can update the Max_budget__c object of it. I was thinking about storing it in a Map with Customer__c as key and List (of Currency__c) as value, But I'm not sure how to do this in a single SOQL Query or if i'm even looking in the right direction. Any ideas? Thanks in advance
You can add a roll-up summary field to your
Customer__c object that contains the MAX (or SUM) of the related detail
Project.Max_Budget__c fields. The platform will maintain that value for you. This works on currency fields.
By having it in your data model, you can display it on the screen or query it to reference it in your logic e.g.;
Customer__c customers = [
select Name, Max_Budget_Rollup__c
order by Max_Budget_Rollup__c desc