0

Where is the loop hole in my code..

trigger updateSumOfTotal on contact (after insert, after update) {
    set<Id> toUpdate = new set<Id>();
    List <Account> toUpdateDML = new List<Account>();
    Map<Account,List<contact>> accToOpp = new map <Account,List<contact>>();
    public Decimal annualrevenue;
    for(contact o : trigger.new) {
        toUpdate.add(o.AccountId);
    }
    for(Account a : [SELECT Id,LightiningName__Sum_of_Total__c,(SELECT ID,LightiningName__Amount__c FROM contacts) from Account WHERE Id IN : toUpdate]) {
        accToOpp.put (a, a.contacts);
        annualrevenue=0;
        for(contact ooo : accToOpp.get(a)) {
            if(ooo.LightiningName__Amount__c!=null){
                annualrevenue +=ooo.LightiningName__Amount__c;
            }
        }
        a.LightiningName__Sum_of_Total__c = annualrevenue;
        toUpdateDML.add(a);
    }       
    if(toUpdateDML.size()>0){ 
        update toUpdateDML; 
    }
}
  • What error you are getting, line no etc? – Mahmood Nov 14 '17 at 8:07
  • iam facing the error during after update event.for after insert event its working fine – saritha sari Nov 14 '17 at 8:21
  • Because you are updating accounts the are in the trigger causing an infinite trigger loop – Eric Nov 14 '17 at 8:22
  • can you please help to control – saritha sari Nov 14 '17 at 8:25
  • problem resolved with boolean variable – saritha sari Nov 14 '17 at 8:33

Browse other questions tagged or ask your own question.