• R Sellers
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;
My issue seems to be very strange. I have created this process and when I test it, it is updating the sales price to deposit amount as said in the requirement. But when the chellenge is checked it gives me error message:
Challenge Not yet complete... here's what's wrong: 
The Fulfillment Cancellation Automation process does not appear to be working properly. Make sure that a cancelled Fulfillment updates the Adventure Package correctly.


Chose the fullfillment object
have proper conditions:
User-added image

and update the adventure package
User-added image

I tested it and it seems to be updating the sales price to the diposit amount correctly:
User-added image

but still on checking Chellenge I get the error:
User-added image

completedly stumped!!! please let me know if anybody faced the same issue. any help.