You need to sign in to do that
Don't have an account?
SFDC 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.
Below is the helper class:
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; } }
you are passing Map<Id, Contact> newContactMap, Map<Id, Contact> oldContactMap as parameters. but where are you getting these values from?
trigger contactAmountSum on Contact (after insert, after delete, after undelete, after update) {
contactAmountSumHelperClass.updateContactCountonAccount(trigger.newMap, trigger.oldMap);
}
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