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
Lakshmi SLakshmi S 

Aggregate Result Error

Hi Team,

I have written a trigger for Rollup summary.
In my code, it doesn't calculte the value if parent object doesn't have child records.
code :
----------
public class KingsleyRollupSummaryTrigHandlerCls {
    
    Public static String recordYear = '2017';

    Public Static void deleteRecords(List<KingsleyTranscript__c> delrecs){
        
        try{
             Set<String> accIds = new Set<String>(); 
        for(KingsleyTranscript__c kt : delrecs){
            if(kt.Account_Name__c != null){
                system.debug('-------'+kt.Account_Name__c);
                accIds.add(kt.Account_Name__c);
            }
        }

            if(accIds.size() > 0){
                
            Map<Id,Account> accountMap = new Map<Id,Account>([Select id,RS_Reg_Client_Satisfaction_Ams_Sum__c,RS_Reg_Client_Satisfaction_Ams_Count__c,RS_Reg_Client_Satisfaction_EMEA_Sum__c,
                               RS_Reg_Client_Satisfaction_EMEA_Count__c,RS_Reg_Client_Satisfaction_APAC_Sum__c,RS_Reg_Client_Satisfaction_APAC_Count__c,
                               RS_Overall_Satisfaction_Rating_Sum__c,RS_Overall_Satisfaction_Rating_Count__c,RS_Value_Contribution_Rating_Sum__c,
                               RS_Value_Contribution_Rating_Count__c,RS_AD_Overall_Rating_Sum__c,RS_AD_Overall_Rating_Count__c,RS_TM_PA_Overall_Rating_Sum__c,
                               RS_TM_PA_Overall_Rating_Count__c,RS_TM_Leader_Rating_Sum__c,RS_TM_Leader_Rating_Count__c,RS_TM_PA_Off_Acct_Leader_Rating_Sum__c,
                               RS_TM_PA_Off_Acct_Leader_Rating_Count__c,RS_FM_Overall_Rating_Sum__c,RS_FM_Overall_Rating_Count__c,RS_FM_Leader_Rating_Sum__c,
                               RS_FM_Leader_Rating_Count__c,RS_FM_Off_Acct_Leadership_Rating_Sum__c,RS_FM_Off_Acct_Leadership_Rating_Count__c,RS_PJM_Overall_Rating_Sum__c,
                               RS_PJM_Overall_Rating_Count__c,RS_PJM_Leader_Rating_Sum__c,RS_PJM_Leader_Rating_Count__c,RS_PJM_Off_Acct_Leadership_Rating_Sum__c,
                               RS_PJM_Off_Acct_Leadership_Rating_Count__c,RS_REMC_Overall_Rating_Sum__c,RS_REMC_Overall_Rating_Count__c,RS_Off_Acct_REMC_Leader_Rating_Sum__c,
                               RS_Off_Acct_REMC_Leader_Rating_Count__c,RS_Thought_Leadership_progs_Rating_Sum__c,RS_Thought_Leadership_prog_Rating_Count__c,
                               RS_Exec_Leadership_Overall_Rating_Sum__c,RS_Exec_Leadership_Overall_Rating_Count__c,RS_Peer_Recommendation_NPS_Rating_Sum__c,RS_Peer_Recommend_NPS_Rating_Count__c from Account where Id IN :accIds]);
            
            Account a = null;
              
                  for(AggregateResult agResult : [Select Account_Name__c,Recorded_Year__c, SUM(Regional_Client_Satisfaction_Americas__c)amsum,COUNT(Regional_Client_Satisfaction_Americas__c)amcount,COUNT(Regional_Client_Satisfaction_APAC__c)apaccount,SUM(Regional_Client_Satisfaction_APAC__c)apacsum,COUNT(Regional_Client_Satisfaction_EMEA__c)emeacount,SUM(Regional_Client_Satisfaction_EMEA__c)emeasum from 
                                                  KingsleyTranscript__c where Account_Name__c IN :accIds and Recorded_Year__c =:recordYear Group By Account_Name__c,Recorded_Year__c]){
                                                      
                         String aId = (String)agResult.get('Account_Name__c');
                         a=accountMap.get(aId);
                         a.RS_Reg_Client_Satisfaction_Ams_Sum__c = (Decimal)agResult.get('amsum');
                         a.RS_Reg_Client_Satisfaction_Ams_Count__c = (Integer)agResult.get('amcount');
                         a.RS_Reg_Client_Satisfaction_APAC_Sum__c = (Decimal)agResult.get('apacsum');
                         a.RS_Reg_Client_Satisfaction_APAC_Count__c = (Integer)agResult.get('apaccount');
                         a.RS_Reg_Client_Satisfaction_EMEA_Sum__c = (Decimal)agResult.get('emeasum');
                         a.RS_Reg_Client_Satisfaction_EMEA_Count__c = (Integer)agResult.get('emeacount');
                         //accountsToUpdate.add(a);
                         accountMap.put(aId, a);
          
                       }
                
        update accountMap.values();
                
            }  

        }
        catch(Exception ex){
            ex.getMessage();
        }
    }
    
}
Please let me know how to do this..........

Regards
Lakshmi

 
arpit vijayvergiyaarpit vijayvergiya
Hello Lakshmi,
As I understand your problem, you want that if an account does not have any record of KingsleyTranscript__c then rollup fields of that object should be set to 0. If yes then you should save calculation of aggregate result in a map and then iterate account map and set all field initially with 0 and then get the value from the aggregate result map and set on appropriate fields then update the account

Thanks,