You need to sign in to do that
Don't have an account?
Jesus G
Roll-up summary field Account-Cases
Hello,
Please, could you help me to edit the following class so instead of updating the 'Total_Hours__c' Account field with the number of related Cases, it is updated with the sum of the 'Hours__c' field of its related Cases?
J
Please, could you help me to edit the following class so instead of updating the 'Total_Hours__c' Account field with the number of related Cases, it is updated with the sum of the 'Hours__c' field of its related Cases?
public class CasesTriggerHandler { public static void handleAfterInsert(List<Case> CasesList){ Set<Id> setAccountIds = new Set<Id>(); List <Account> AccountsToUpdate = new List <Account>(); for (Case currentCase : CasesList) { setAccountIds.add(currentCase.AccountId); } Map<Id, Account> AccountMap = new Map<Id, Account> ([SELECT Id, Total_Hours__c FROM Account WHERE Id IN: setAccountIds]); for (Account currentAccount : [SELECT Id,Total_Hours__c, (SELECT Id FROM Cases) FROM Account WHERE Id IN : setAccountIds]){ AccountMap.get(currentAccount.Id).Total_Hours__c = currentAccount.Cases.size(); AccountsToUpdate.add(AccountMap.get(currentAccount.Id)); } update AccountsToUpdate; } }Many thanks!
J
Please use below mentioned code:
All Answers
Please use below mentioned code:
Many thanks for your reply.
I am afraid I get an error since the 'Decimal' variable (line 12) does not exist. Where would I need to define it, please?
Jesus
Sorry, I made a mistake here. Use this line instead at line no.12:
AccountsToUpdate.add(new Account(Id = Id.valueOf(String.valueOf(arr.get('acc'))),Total_Hours__c = Double.valueOf(arr.get('sum'))));