+ Start a Discussion
shiva mallishiva malli 

How to calculate sum on child object to display in parent object

Hi friends,

I am writing sum trigger between two custom objects and program is saved but not executed properly like argument can't be null here i am passing values but it throws an error
 Trigger sum_of_amount on Employee__c(after insert,after update,after delete,after undelete)
 {
 List<Department__c> dept_amount=new List<Department__c>();
 set<id> Dept_id=new set<id>();
 for(Employee__c emp:trigger.new)
 {
 Dept_id.add(emp.deptid__c);
 }
 Decimal adding_amount;
 for(Department__c dept:[select id,deptname__c,(select id,EmpName__c,EmpSal__c from Employees__r) 
 from Department__c where id in:dept_id])
 {
 adding_amount=0;
 for(Employee__c emplist:dept.Employees__r)
 {
 system.debug('My Emp Salary value is +++++===='+emplist);
 adding_amount+=emplist.EmpSal__c;
 system.debug('Newly adding value is========++++++'+adding_amount);
 }
 dept.Total_amount__c=adding_amount;
 dept_amount.add(dept);
 }
 update dept_amount;
 }
Arpit Jain7Arpit Jain7
Hey Shiva,

Please put filter that EmpSal__c field is not blank before the below line in your code

"adding_amount+=emplist.EmpSal__c;" 

As this might be the reason that  you are receiving argument can not be null error.

Also you can try to use aggregate query for qchieving this.

Please mark as best answer if it resolved your query. Let me know for any other query.

Thanks
Bhavana RBhavana R
Hi Shiva,
I think you can use roll-up summary for this requirement.
Also in line 11 of the code you have used dept_id. Could you tell me more about that variable? Previously you have use Dept_id set.
Thanks
VineetKumarVineetKumar
Try the below code :
Trigger sum_of_amount on Employee__c(after insert,after update,after delete,after undelete){
	List<Department__c> dept_amount=new List<Department__c>();
	set<id> Dept_id=new set<id>();
	for(Employee__c emp : trigger.new){
		if(emp.deptid__c != null){
			Dept_id.add(emp.deptid__c);
		}
	}
	if(!Dept_id.isEmpty()){
		Decimal adding_amount;
		for(Department__c dept:[select id,deptname__c,(select id,EmpName__c,EmpSal__c from Employees__r) 
								 from Department__c where id in:Dept_id]){
			adding_amount=0;
			for(Employee__c emplist:dept.Employees__r){
				system.debug('My Emp Salary value is +++++===='+emplist);
				if(emplist.EmpSal__c != null){
					adding_amount+=emplist.EmpSal__c;
				}
				system.debug('Newly adding value is========++++++'+adding_amount);
			}
			dept.Total_amount__c=adding_amount;
			dept_amount.add(dept);
		}
		if(!dept_amount.isEmpty()){
			update dept_amount;
		}
	}
 }
UC InnovationUC Innovation
I agree with Bhavana.  If the Employee and Department objects are Master/Detail, then Total_amount__c should just be a rollup field.  Code is not needed.
Jitendra pratap singhJitendra pratap singh
Hi Shiva,
Try this trigger. Its surely works on every operation Insert, Update and Delete. You can replace your object name respectively with Account and Contact.


trigger rollUpSummryOnConact on Contact(After update,After insert,after delete,after undelete) 
{
    Set<Id> accIdset = new Set<Id>();
    if(trigger.isinsert || trigger.isUpdate || trigger.isundelete)
    {
        for(contact con : trigger.new)
        {
            if(con.AccountId != null)
            {
                accIdset.add(con.AccountId); 
            }
        }
    }
    else if(trigger.isdelete)
    {  
        for(contact con : trigger.old)
        {
            if(con.AccountId != null)
            {
                accIdset.add(con.AccountId);
            }
        }
    
    }  
   List<AggregateResult>  lstAggregation = new list<AggregateResult>();

   lstAggregation  = [select COUNT(Id) countId,AccountId accId from Contact Where AccountId  =: '0019000001c5FpO' group by AccountId]; 
   List<Account> lstAccout = new List<Account>();
   for(AggregateResult objAggregateResult : lstAggregation )
   {
      Account acc = new Account();
      acc.id = String.valueOf(objAggregateResult.get('accId'));
       acc.TotalNoOfcontact__c = Integer.valueOf(objAggregateResult.get('countId'));
      lstAccout .add(acc);      
   }
   
   if(lstAccout.size()>0)
   update lstAccout ;
}

Jitendra Pratap Singh
www.mirketa.com