Salesforce : Rollup Summary implementation using apex for count.

The use case is to get the count of all child contacts at the parent account level.

In this use case we will have four scenarios:

Insert : When new contact is created for an account, in this case the count of number of contacts should increase on the account record for every new contact.

Update : When contact is moved from one account to another, in this case the count of number of contact should increase on the new parent account and decrease on the old parent account.

Delete : When contact record is deleted for an account, in this case the count of contact should decrease on the account record for every deleted contact.

UnDelete : When contact record is un-deleted for an account, in this case the count of contact should increase on the account record for every contact record getting un-deleted.


Solution : For this use case we need to write the calculation logic for counting number of contacts on after insert, after update, after delete and after undelete events of contact trigger as we need to update the related object data i.e. account object.

Note: The logic written here is only for calculating the count of contact but no such trigger framework is followed in the below code snippet.

Consideration : If this logic is not built at starting that is even before creating a single contact record, then we would require to perform a one time data load activity to update the number of contacts count on account object record for the below logic to work as expected.


Trigger ContactTrigger on Contact(after insert, after update, after delete, after undelete){

    Map<Id,Integer> accConCountMap = new Map<Id,Integer>();
    Id oldAccountId;
    for(Contact con:Trigger.isDelete?Trigger.old:Trigger.new){
        if(Trigger.isUpdate && con.AccountId != trigger.oldmap.get(con.Id).AccountId){
            oldAccountId = trigger.oldmap.get(con.Id).AccountId;
            //For increasing the count of no. of contacts for new account.
            if(accConCountMap.containsKey(con.AccountId)){
                accConCountMap.put(con.AccountId,accConCountMap.get(con.AccountId)+1);
            } else {
                accConCountMap.put(con.AccountId,1);
            }
            //For reducing the count of no. of contacts for old account.
            if(accConCountMap.containsKey(oldAccountId)){
                accConCountMap.put(oldAccountId,accConCountMap.get(oldAccountId)-1);
            } else {
                accConCountMap.put(oldAccountId,-1);
            }
        } else if(!Trigger.isUpdate) {
            if(trigger.isDelete){
                if(accConCountMap.containsKey(con.AccountId)){
                    accConCountMap.put(con.AccountId,accConCountMap.get(con.AccountId)-1);
                } else {
                    accConCountMap.put(con.AccountId,-1);
                }
            } else {
                if(accConCountMap.containsKey(con.AccountId)){
                    accConCountMap.put(con.AccountId,accConCountMap.get(con.AccountId)+1);
                } else {
                    accConCountMap.put(con.AccountId,1);
                }
            }
        }
    }
    List<Account> accList = new List<Account>();
    Account acc;
    //Considering we have a Number type field on account object named Contact counts (Contact_counts__c) which stores the number of contact.
    for(Account acc:[Select Id,Contact_counts__c From Account where Id IN : accConCountMap.keyset()]){
        acc = new Account(Id = acc.Id,Contact_counts__c = accConCountMap.get(acc.Id)+(acc.Contact_counts__c == null ? 0 : acc.Contact_counts__c));
        accList.add(acc);
    }

    if(!accList.isEmpty()){
        update accList; 
    }

}        



To view or add a comment, sign in

Others also viewed

Explore content categories