function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jesus GJesus 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?
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
Best Answer chosen by Jesus G
Pankaj_GanwaniPankaj_Ganwani
Hi Jesus,

Please use below mentioned code:
 
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);
        }
        
		for(AggregateResult arr : [select SUM(Hours__c) sum,AccountId acc from Case where AccountId In : setAccountIds Group by AccountId])
		{
			AccountsToUpdate.add(new Account(Id = Id.valueOf(String.valueOf(arr.get('acc'))), Total_Hours__c = Decimal.valueOf(arr.get('sum'))));
		}
		update AccountsToUpdate;
    }
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Jesus,

Please use below mentioned code:
 
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);
        }
        
		for(AggregateResult arr : [select SUM(Hours__c) sum,AccountId acc from Case where AccountId In : setAccountIds Group by AccountId])
		{
			AccountsToUpdate.add(new Account(Id = Id.valueOf(String.valueOf(arr.get('acc'))), Total_Hours__c = Decimal.valueOf(arr.get('sum'))));
		}
		update AccountsToUpdate;
    }
}

 
This was selected as the best answer
Jesus GJesus G
Hello Pankaj,

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
Pankaj_GanwaniPankaj_Ganwani
Hi,

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'))));