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
SFDC GuestSFDC Guest 

trigger rollup summary

Hi All, Can you please help me to convert below logic into helper class. I am getting exception as attempt to deference null object.  Thanks.
 
trigger contactAmountSum on Contact (after insert, after delete, after undelete, after update) {
    set<Id> accIdSet = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
        for(Contact conObj: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (conObj.Amounts__c != Trigger.oldMap.get(conObj.Id).Amounts__c || conObj.AccountId != Trigger.oldMap.get(conObj.Id).AccountId))
				accIdSet.add(conObj.AccountId);            
        }
    }    
    if(trigger.isUpdate || trigger.isDelete) {
        for(Contact conObj: Trigger.old){
            if(Trigger.isDelete || (conObj.Amounts__c != Trigger.newMap.get(conObj.Id).Amounts__c || conObj.AccountId != Trigger.newMap.get(conObj.Id).AccountId))
				accIdSet.add(conObj.AccountId);
        }
    }       
    List<Account> accList = [select id, Amounts__c, (Select Id, Amounts__c from contacts) from Account Where ID IN: accIdSet];    
    for(Account acc : accList){
        
        acc.Amounts__c = 0;
        for(Contact conObj : acc.contacts) {
			acc.Amounts__c += conObj.Amounts__c;
        }
    }
    update accList;
}

Below is the helper class:
 
public class contactAmountSumHelperClass
{
    public static void updateContactCountonAccount(Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap)
    {
        Set<Id> accIdSet = new set<Id>();
                
        for(Contact con: newContactMap.values())
        {
            if(con.AccountId != null){
                if(con.Amounts__c != oldContactMap.get(con.Id).Amounts__c ||
                    con.AccountId != oldContactMap.get(con.Id).AccountId){
                    accIdSet.add(con.AccountId);
                }
            }
        }
        
        List<Account> accList = [select id, Amounts__c,Description, (Select Id, Amounts__c from contacts ) from Account 
                                 Where ID IN: accIdSet ];    
        for(Account acc : accList)
        {        
            acc.Amounts__c = 0;
            for(Contact conObj : acc.contacts)
            {  
                acc.Amounts__c += conObj.Amounts__c; 
            }
        }
        update accList; 
    }
}

 
Rounak SharmaRounak Sharma
hello,
you are passing Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap as parameters. but where are you getting these values from?

 
SFDC GuestSFDC Guest
I am calling as below.

trigger contactAmountSum on Contact (after insert, after delete, after undelete, after update) {
contactAmountSumHelperClass.updateContactCountonAccount(trigger.newMap, trigger.oldMap);
}
Rounak SharmaRounak Sharma
hi,
No you are not getting my point. You have passed
Map<Id, Contact> newContactMap and Map<Id, Contact> oldContactMap as parameters in updateContactCountonAccount method.
So where are you getting their values from.
Either you are getting the values from trigger?
Or you need to query the values from database.

Please let me know
thanks