You need to sign in to do that
Don't have an account?
shiva 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;
}
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;
}
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
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
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