Custom Rollup summaries using trigger salesforce


Trigger Code :
--------------
trigger RollupSummaries_Trg on Contact (after insert,after update,after delete,after undelete) {
    //RollupSummaries_Helper.cmdRollupCalculation(trigger.newMap.Values(),trigger.isInsert,trigger.isUpdate,trigger.isAfter);
    RollupSummaries_Helper.cmdRollupCalculation(trigger.new,trigger.isInsert,trigger.isUpdate,trigger.isAfter,trigger.isDelete,trigger.isUndelete,trigger.old);
}

Helper Class :
--------------
public class RollupSummaries_Helper {
    public static Map<Id,contact> uniqueMap = new Map<Id,contact>();
    public static List<Account> finalAcc = new List<Account>();
    public static Set<Contact> conSet = new Set<Contact>();
    public static Decimal sumCount;
    public static Decimal sumMax;
    public static Decimal sumMin;
    public static Decimal sumAvg;
    public static decimal sumCountList;
    public RollupSummaries_Helper(){
    }
    public static void cmdRollupHelper(List<Contact> triggerNew){
            for(contact con : triggerNew){
              uniqueMap.put(con.AccountId,con);
          }
            for(account a : [select id,(select id from contacts) from account where id IN : uniqueMap.keySet()]){
                conSet.addAll(a.contacts);
          }
        
            List<AggregateResult> groupedResults = [select count(id) totalCount,sum(Amount__c) totalSum,Max(amount__c) totalMax,min(amount__c) totalMin,avg(amount__c) totalAvg from contact where ID IN :conSet];
            for(AggregateResult ar : groupedResults ){
                sumCount = (Decimal)ar.get('totalSum');
                sumMax = (Decimal)ar.get('totalMax');
                sumMin = (Decimal)ar.get('totalMin');
                sumAvg =  (Decimal)ar.get('totalAvg');
                sumCountList = (Decimal)ar.get('totalCount');
            }
             for(account a : [select id,Sum_Of_Contacs__c from account where id IN : uniqueMap.keySet()]){
                 a.Rollup_Sum__c = sumCount;
                 a.Sum_Of_Contacs__c =sumCount;
                 a.Rollup_Max__c = sumMax;
                 a.Rollup_Min__c = sumMin;
                 a.Rollup_Avg__c = sumAvg;
                 a.Rollup_Count__c = sumCountList;
                 
                 finalAcc.add(a);
             }
        if(!finalAcc.isEmpty()){            
            database.update(finalAcc,false);
        }
    }
    public static Set<Id> acctIds = new Set<Id>();
    public static void cmdRollupCount(List<Contact> triggerNew,List<Contact> triggerOld){
        for(Contact c : triggerNew){
      acctIds.add(c.AccountId);            
        }
        Map<Id,Account> acctMapIds = new Map<Id,Account>([select id,Rollup_Count__c,(select id from contacts) from account where id IN :acctIds]);
        for(Account a : acctMapIds.values()){
            a.Rollup_Count__c = a.contacts.size();
        }
        if(!acctMapIds.isEmpty()){
            Database.update(acctMapIds.values(),false);
        }
    }
    public static void cmdRollupCountDelete(List<Contact> triggerNew,List<Contact> triggerOld){
        for(Contact c : triggerOld){
      acctIds.add(c.AccountId);            
        }
        Map<Id,Account> acctMapIds = new Map<Id,Account>([select id,Rollup_Count__c,(select id from contacts) from account where id IN :acctIds]);
        for(Account a : acctMapIds.values()){
            a.Rollup_Count__c = a.contacts.size();
        }
        if(!acctMapIds.isEmpty()){
            Database.update(acctMapIds.values(),false);
        }
    }
    public static void cmdRollupCalculation(List<Contact> triggerNew,boolean isInsert,boolean isUpdate,boolean isAfter,boolean isDelete,boolean isUndelete,List<Contact> triggerOld){        
        if(isAfter){
            if(isInsert){
                cmdRollupHelper(triggerNew);
                //cmdRollupCount(triggerNew,triggerOld);
            }else if(isUpdate){
                cmdRollupHelper(triggerNew);
            }else if(isUndelete){
                cmdRollupHelper(triggerNew);
            }else if(isDelete){
                cmdRollupCountDelete(triggerNew,triggerOld);
            }
        }
    }
}

Featured

What is Cryptography in salesforce and what are all the algorithms provided by them ?

A). It is a security protocal between two systems. Lets say we are integration two systems without any encrytion mechanism then hackers wil...

Popular