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
raji ch 8raji ch 8 

Write a Trigger on Account Object having Custom field Total salary and Associated contacts having salary field? (insert , upadate, delete, undelete)

Aamir KhanAamir Khan

Please follow the code below:
trigger RollUpSummaryTrigger on Contact (after insert, after update, after delete, after undelete) {
    Set<Id> accSet = new Set<Id>();
    for(Contact c :{
    Map<Id,Account> accMap = new Map<Id,Account> ();
    for(Account acc : [SELECT id, Total_Salary__c FROM Account WHERE id IN :accSet]){

    for(Contact con :{

        accMap.get(con.AccountId).Total_Salary__c = accMap.get(con.AccountId).Total_Salary__c + con.Salary__c;
    upsert accmap.values();
Make sure Salary and Total Salary field are currency or number fields.

Hope this helps.

Please mark Solved if it works.

Aamir AH Khan
Lead Salesforce Lightning Developer
Manish  ChoudhariManish Choudhari
Hi Raji,

Not sure why you are looking trigger for this functionality. You can use RollUp Summary field to achieve same functionality withou writing any code.
Just create new field on Account, select field type as "RollUp Summary" field. You can choose "SUM"  method and specifiy contact salary field there.

More on this:

Please let me know if you need further help on this.

Jainam ContractorJainam Contractor
Hi Raji,

Please find the below code for Rolling up the Salary field from contact to the Total Salary on the Account Object.

The trigger by Amir doesn't take care of the delete operation also it doesn't consider the scenario of the Contact not related to any account i.e AccountID field is NULL and thus it would fail while you try to test the trigger by deleting the record or update/ insert the contact without the related account record.

@Manish: Roll-Up summary fields aren't available for Lookup Relationship and Account-Contact is one of them. Account-Opportunity is an exception, which was delivered later.

The below code takes care of all the Scenario and also abides by all the Governor limit.
trigger UpdateAccSalary on Contact (after insert, after update, after delete, after undelete) {
    Map<ID,Account> accountMap = new Map<ID,Account>(); 
    List<ID> accID = new List<ID>();
    //Check for type of DML Operation, For Insert, Update and Undelete we have Trigger.New
    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){
        for(Contact C1:{
            //Add all new AccountID in the List 
            if(C1.AccountID != NULL)
    //Check for type of DML Operation, For Delete we have Trigger.Old
        for(Contact C2: Trigger.old){
            if(C2.AccountID != NULL)
    //Check if List is not empty
    if(accID!=NULL && accID.size()>0){
        //Add all the accounts in the Map, to map ID with the Account Cost
        for(ID AccountID : accID){
            accountMap.put(AccountID, new Account(ID = AccountID, Total_Salary__c = 0));
        //Calculate the Total Account Cost based on the Contact Cost
        for(Contact C : [SELECT ID, AccountID, Salary__c FROM Contact WHERE AccountID IN :accID]){
            accountMap.get(C.AccountID).Total_Salary__c += c.Salary__c;
        //Commit to the Database

Please mark this as the solution if it solved your purpose.

Also if possible look-out the tech blog released by the Varasi Team for the similar scenario on the below link. It might be able to help you on your future implementation.


Jainam Contractor,
Salesforce Consultant,
Varasi LLC

Mohammed SadathMohammed Sadath
Hi Sir, I tried your code that you had shown on the screen to help raji ch 8 but i am getting an exception namely " UpdateAccSalary: execution of AfterUpdate caused by: System.NullPointerException: Argument cannot be null. Trigger.UpdateAccSalary: line 29, column 1"

User-added imageUser-added imageUser-added imageUser-added image
Mohammed SadathMohammed Sadath
Hi @Jainam sir please resolve this issue