function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chitral ChaddaChitral Chadda 

trigger to sum

sum__c field in contact and total_sum__c field in account
for all the contact fr a particular account 
i want the total sum to be displayed in total_sum__c field
like in 1 contact sum__c field has 10
in other contact sum__c field is 20
so total_sum__c should be 30

i dont want to use aggregate funt and roll up summary?
any idea


trigger sum_Total_Sum on Contact (after insert,after update ,after delete)
{
    set<id> conAccId = new set<id>();
   
              if(trigger.isInsert || trigger.isUpdate)
              {
                  for(contact c : trigger.new)
                    {
                          conAccId.add(c.AccountId);
                     
                    }
               }
      
      
            if(trigger.isDelete)
            {
               for(contact c: trigger.old)
                {
                conAccId.add(c.AccountId);
                }
            }
           
       
      List<account> accountToUpdate = new List<account>();
     
 
       for( account ac :[select id,(select id, Sum__c from contacts ) from account  where id IN : conAccId ])
        {
                      
                      
                     ac.Total_Sum__c =ac.Total_Sum__c + c.Sum__c;
                   
                    accountToUpdate.add(ac);
              
        }
     update accountToUpdate;
    
  }
Best Answer chosen by Chitral Chadda
Rajendra ORajendra O
Yes you should be using aggregate query, as stated above but still if you want to do this as per your code then replace few lines as follows:-
for( account ac :[select id,(select id, Sum__c from contacts ) from account  where id IN : conAccId ]){
	 ac.Total_Sum__c = 0;
	 for(Contact c:ac.contacts){
		ac.Total_Sum__c = ac.Total_Sum__c + c.Sum__c;
	 }             
	accountToUpdate.add(ac);
}

Reason : The error you are getting because contact is actually wrapper under account, so you will need to loop over that collection. Hope this solve the issue

All Answers

AshwaniAshwani
What issue you are getting with this trigger. Can't you use Aggregate query to make it simpler? This requirement should be done throw aggregate query.
Chitral ChaddaChitral Chadda
Variable does not exist: con.Sum__c at line 31 column 57
i know it can b done thru aggrgate quey brt i need to do this way
Rajendra ORajendra O
Yes you should be using aggregate query, as stated above but still if you want to do this as per your code then replace few lines as follows:-
for( account ac :[select id,(select id, Sum__c from contacts ) from account  where id IN : conAccId ]){
	 ac.Total_Sum__c = 0;
	 for(Contact c:ac.contacts){
		ac.Total_Sum__c = ac.Total_Sum__c + c.Sum__c;
	 }             
	accountToUpdate.add(ac);
}

Reason : The error you are getting because contact is actually wrapper under account, so you will need to loop over that collection. Hope this solve the issue
This was selected as the best answer
Chitral ChaddaChitral Chadda
perfect