User Story
As a Salesforce administrator,
I want to update the annual revenue of each account based on the sum of amounts from associated opportunities, So that the account records reflect the aggregated financial data accurately.
Codes
List<AggregateResult> result = [SELECT AccountId Idd, sum(Amount) sum FROM Opportunity Group BY AccountId];
List<Account> acList = new List<Account>();
for(AggregateResult res : result){
Id acId = (Id)res.get('Idd');
Account ac = [SELECT Id, AnnualRevenue FROM Account WHERE Id =: acId];
ac.AnnualRevenue = (Decimal)res.get('sum');
acList.add(ac);
}
update acList;
By Dilek Uslu
List<Account> acList = [SELECT Id FROM Account];
for(Account ac : acList){
List<AggregateResult> result = [SELECT SUM(Amount) FROM Opportunity
WHERE AccountId =: acId];
ac.AnnualRevenue = (Decimal)result[0].get('expr0');
}
update acList;
Video
Video does not exists.