Skip to main content
trigger RollUpSummaryTrigger on Contact (after insert, after update, after delete, after undelete) {

    Set<Id> accountId =  new Set<Id>();

    

    

if(Trigger.isInsert && Trigger.isAfter) {     

    for(Contact contactObj  : Trigger.new){

       accountId.add(contactObj.AccountId); 

    }

    List<Account> accountList = [Select Id,    Totel_Amount__c,(Select Id ,LastName , AccountId ,     Salary__c FROM Contacts) From Account Where Id IN: accountId];

  

    List<Account> accountLists = New  List<Account>();

    for(Account accountObj  : accountList){

         double  sumAmount = 0;

        for(Contact objcontact : accountObj.contacts){

          sumAmount += objcontact.Salary__c; 

        }

        accountObj.Totel_Amount__c = sumAmount;

        accountLists.add(accountObj);

    } 

    

    if(accountLists.Size() > 0){

     Update accountLists;   

    }

 }

}
2 answers
  1. Sep 15, 2021, 6:36 AM
    Hi Mohmmed,

    Can you let the community know what issue you are facing with the above code and what is the functionality you are trying to achieve. 

    Thanks,

     
0/9000