You need to sign in to do that
Don't have an account?
Vivek 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;
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;
You can refer the below code for After insert,after update,after delete,after undelete trigger events.
Hope it Helps! Please mark the aswer as correct if it solves the issue.
Thanks! :)
All Answers
still getting same issue
Please help
This blog is exact what you need.
https://blogs.absyz.com/2016/03/16/roll-up-summary-without-a-master-detail-relationship/
I hope this helps you.
You can refer the below code for After insert,after update,after delete,after undelete trigger events.
Hope it Helps! Please mark the aswer as correct if it solves the issue.
Thanks! :)
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