+ Start a Discussion
Vivek NayakVivek Nayak 

Need Urgent help:Trigger to populate sum of child records

Hi Everyone;
In each interview i am being asked this question but i am unable to write and run it properly.
Here is question:
write a trigger which will populate sum of child field values on prarent.(basically count Rollup summary trigger for lookup relationship)
I did search in community and got below program which is still not working:

trigger SumTrigger on Contact (before insert) {
list<Account>accts=new list<Account>();
set<ID>IDs=new set <ID>() ;
  if(trigger.isInsert||trigger.isUndelete){
      for(contact con:trigger.new) {  
      IDs.add(con.accountId) ;      
      }}   
    
  else if(trigger.isDelete){
      for(contact con:trigger.old) {  
      IDs.add(con.accountId) ;      
      }}   
 
else if(trigger.isUpdate){
      for(contact con:trigger.new) {
          if(con.AccountId !=null){
           IDs.add(con.accountId) ;   
          }     
            
      }}   
      
  if(IDs.size()>0){
 accts=[select id,TotalValue__c,(select id,value__c from contacts ) from Account where id in :IDs];              
  }     

  for(Account a:accts)  {
    for (contact c:a.contacts){
    a.TotalValue__c +=c.value__c;    
    }
        update accts; 
    }
}

No complie error but when inserting contact record getting below error:

SumTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.SumTrigger: line 30, column 1

also on modifying a section like below 

for(Account a:accts)  {
    decimal Value=0;
    for (contact c:a.contacts){
    value += c.value__c;
        
    }
    a.TotalValue__c =value;    
        
    }
     update accts;                 
}

again getting below error on insering any contact record:
SumTrigger: execution of BeforeInsert caused by: System.NullPointerException: Argument cannot be null. Trigger.SumTrigger: line 29, column 1

Please help me with easy trigger code to make it work.

Thanks in advance;
Best Answer chosen by Vivek Nayak
Umesh RathiUmesh Rathi
Hi Vivek,

You can refer the below code for After insert,after update,after delete,after undelete trigger events.
Set<id> accIds = new Set<id>();
        List<Account> lstAccToUpdate = new List<Account>();
        for(Contact c: lstCon){
            if(c.AccountId!=null){
                accIds.add(c.AccountId);
            }
        }
        for(Account a : [select id,name,Total_Salary__c,(select id,name,salary__c from Contacts) from Account where id IN : accIds]){
            a.Total_Salary__c=0;
            for(Contact con : a.Contacts){
                System.debug('con-->'+con);
                if(con.salary__c!=null){
                    a.Total_Salary__c = a.Total_Salary__c + con.salary__c;
                }            
            }
            lstAccToUpdate.add(a);
        }
        update lstAccToUpdate;

Hope it Helps! Please mark the aswer as correct if it solves the issue.
Thanks! :)

All Answers

Vivek NayakVivek Nayak
sorry correct events are (after insert,after delete,after undelete,after update)
still getting same issue
Please help
Abhishek Raj 23Abhishek Raj 23
Umesh RathiUmesh Rathi
Hi Vivek,

You can refer the below code for After insert,after update,after delete,after undelete trigger events.
Set<id> accIds = new Set<id>();
        List<Account> lstAccToUpdate = new List<Account>();
        for(Contact c: lstCon){
            if(c.AccountId!=null){
                accIds.add(c.AccountId);
            }
        }
        for(Account a : [select id,name,Total_Salary__c,(select id,name,salary__c from Contacts) from Account where id IN : accIds]){
            a.Total_Salary__c=0;
            for(Contact con : a.Contacts){
                System.debug('con-->'+con);
                if(con.salary__c!=null){
                    a.Total_Salary__c = a.Total_Salary__c + con.salary__c;
                }            
            }
            lstAccToUpdate.add(a);
        }
        update lstAccToUpdate;

Hope it Helps! Please mark the aswer as correct if it solves the issue.
Thanks! :)
This was selected as the best answer
Vivek NayakVivek Nayak
Hi Umesh, thanks a lot for quick help. It working fine.
however, noticed that there is only one change in your code i.e  if(con.salary__c!=null) which fixed  the error . Any idea how it was related to 
that 'System.NullPointerException: Argument cannot be null' error. it would help me.

Thans Again,
Vivek