Trigger to Roll Up (Summary) Contacts field to Accounts

Trigger to create a Roll-up Summary of Contact’s field (salary) on Accounts

Requirement:

  1. The Account object has a field named Total Salary with the API name total_salary__c
  2. The Contact object has a field named Salary with the API name salary__c
  3. On each Account, the Total Salary field should hold the sum of Salaries from its associated Contacts. The trigger should work whenever a Contact’s Salary is updated, New Contact is Created or when an existing Contact is deleted.

Solution:

Let’s write this Trigger in the following flow:

  1. Get a List of Accounts that needs to be updated.
  2. Get a List of Contacts whose Salaries will be added
  3. Create a Map of Accounts and List of associated Contacts
  4. Iterate through each Account to update the Total Salary
    1. Iterate through each associated Contact to add the Salaries.
  5. Update the final List of Accounts using DML

 

Here’s the trigger:

trigger CustRollUpSummaryAccount on Contact (after insert,after update, after delete) {
    
    //New List to perform DML on the final List of Accounts
    List<Account> List2Update = New List <Account>();
    
    //New Set of Account Ids that'd be used to get Accounts & Contacts List
    Set<ID> accountIds = New Set <ID>();
    
    //Using Context variables and populating the above Set
    If (trigger.isInsert || trigger.isUpdate) {
        for (contact c1:Trigger.New){
            accountIds.add(c1.AccountId);
        }
    } else If (trigger.isDelete) {
        for (contact c2:Trigger.Old){
            accountIds.add(c2.AccountId);
        }
    }
    
    //List of Accounts in this trigger populated using SOQL
    List <Account> accountList = New List <Account>();
    accountList = [SELECT Id FROM Account WHERE ID IN:accountIds];
    
    //List of Contacts in this trigger populated using SOQL
    List <Contact> contactList = New List <Contact>();
    contactList = [SELECT id,accountid,salary__c FROM Contact WHERE accountid in:accountIds];
    
    //New Map for Accounts and List of its Contacts
    Map<Id,List<Contact>> accConMap = New Map <Id,List<Contact>>();
    
    //Iterate through Contacts and populate the above map
    For (Contact Con:contactList) {        
        If (!accConMap.keyset().contains(con.AccountId)) {
            accConMap.put(con.AccountId, New List<Contact>());
        }
        accConMap.get(con.AccountId).add(con);
    }
    
    //Iterate through accounts 
    For (Account acc:accountList) {
        Double Amount = 0;    
        
        If (accConMap.get(acc.Id) != null && accConMap.get(acc.Id).size() > 0) {
            //Iterate through List of Contacts and add the amounts in the Salary fields
            For (Contact con1:accConMap.get(acc.Id)){
                If (con1.Salary__c != null) {
                    Amount = Amount + con1.Salary__c;
                }
            }    
            
        }
        acc.Total_Salary__c = Amount; //Update the Total Salary field on Account
        List2Update.add(acc); //add account to final update List
        
    }
    update List2Update; //update the final list of Accounts
}

 

 


Posted

in

by

Comments

3 responses to “Trigger to Roll Up (Summary) Contacts field to Accounts”

  1. musketer Avatar
    musketer

    wow!..really helped me alot

  2. Syntaxx Avatar
    Syntaxx

    big help.thank a lot

  3. Neeraj Saini Avatar
    Neeraj Saini

    accConMap.put(con.AccountId, New List()); not including 1st contact salary’s value in contact list

    accConMap.put(con.AccountId, New List{con});

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.