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
Deepak Hanumantha AndeliDeepak Hanumantha Andeli 

Roll up Summary Requirment

Hi All,

We have a requirement where 2 object Account and Activities (Custom Object) are required to be associated either through a Lookup or Master Detail relationship. The challenge is to utilize the OOTB salesforce feature over Custom coding as much possible.

The requirement is every Account will be associated to multiple Acitivities. Each Activity record will have a field called Points and sum of all the points against associated Activities are to be calculated and rolledup to Account in another Points field.

While at the first look it seems a simple case of Master Detail relationship between Account and Activities and Rollup Summary field to sum up all the points but here is a twist.

The twist is, Accounts are set up in an Hierarchical manner. ie., Account A1 is linked to 2 children Accounts A2 and A3
Both A2 and A3 has their own associated activities. 
Say A2 has 10 points and A3 has 20 points - when one views A1 there should be a value of 30 against Points field in A1.

Considering this hierarchical model, one of the developer has recommended a Look up relationship between the 2 object and Apex trigger each time an activities is created or updated to carry out the calculation.

The question is whether the same can still be achieved by Master detail relationship with lesser custom code and more OOTB feature such as RollUp Summary field.

Anilkumar KotaAnilkumar Kota
Hello Deepak ,

This may help for your requirement .

trigger RollUpSummaryTrigger on StudentT__c(after insert,after delete,after update) {
    
    if(trigger.isInsert && Trigger.isAfter){
        RollupSummary.doRollupSummary(trigger.new);        
    }
    
    if(trigger.isUpdate && Trigger.isAfter){
        List<StudentT__c> myStudList = new List<StudentT__c>();
        myStudList.addAll(Trigger.old);
        myStudList.addAll(Trigger.new);
        RollupSummary.doRollupSummary(myStudList);        
    }
    
    if(trigger.isDelete && Trigger.isAfter){
        RollupSummary.doRollupSummary(trigger.new);        
    }
}




====================================================================


public class RollupSummary
{
    public static void doRollupSummary(List<StudentT__c> studObj)
    {
        list<ID> teacherIDs= New List<ID>();
        Map<ID,Integer> countOfChilds = New Map<ID,Integer>();
        Map<ID,Integer> sumOfChilds = New Map<ID,Integer>();
        Map<ID,Integer> minOfChilds = New Map<ID,Integer>();
        Map<ID,Integer> maxOfChilds = New Map<ID,Integer>();
        
        
        for(integer i=0;i<studObj.size();i++){
              teacherIDs.add(studObj[i].Teacher__c);   
        }
        
        List<StudentT__c> studentTeachers = [SELECT id,Teacher__c,Fee__c FROM StudentT__c where Teacher__c=:teacherIDs];
        
        for(integer j=0;j<studentTeachers.size();j++){
            if(!countOfChilds.containsKey(studentTeachers[j].Teacher__c)){
                countOfChilds.put(studentTeachers[j].Teacher__c,1);
                sumOfChilds.put(studentTeachers[j].Teacher__c,Integer.valueOf(studentTeachers[j].Fee__c));
                minOfChilds.put(studentTeachers[j].Teacher__c,Integer.valueOf(studentTeachers[j].Fee__c));
                maxOfChilds.put(studentTeachers[j].Teacher__c,Integer.valueOf(studentTeachers[j].Fee__c));
                
            }else{
                integer increseCount = countOfChilds.get(studentTeachers[j].Teacher__c) + 1;
                Integer sum = Integer.valueOf(sumOfChilds.get(studentTeachers[j].Teacher__c) + studentTeachers[j].Fee__c);
                countOfChilds.put(studentTeachers[j].Teacher__c,increseCount);
                sumOfChilds.put(studentTeachers[j].Teacher__c,sum);
                
                if(minOfChilds.get(studentTeachers[j].Teacher__c) > Integer.valueOf(studentTeachers[j].Fee__c))
                    minOfChilds.put(studentTeachers[j].Teacher__c,Integer.valueOf(studentTeachers[j].Fee__c)); 
                if(maxOfChilds.get(studentTeachers[j].Teacher__c) < Integer.valueOf(studentTeachers[j].Fee__c))
                    maxOfChilds.put(studentTeachers[j].Teacher__c,Integer.valueOf(studentTeachers[j].Fee__c));   
            }
        }
        
        List<TeacherT__c> clientRollUpupdate = [SELECT id FROM TeacherT__c WHERE id=:teacherIDs];
        List<TeacherT__c> allClientCountupdates = New List<TeacherT__c>();
        
        for(integer k=0;k<clientRollUpupdate.size();k++){
            if(countOfChilds.containsKey(clientRollUpupdate[k].id)){
                clientRollUpupdate[k].Count__c= countOfChilds.get(clientRollUpupdate[k].id);
                
            }
            
            if(sumOfChilds.containsKey(clientRollUpupdate[k].id)){
                
                clientRollUpupdate[k].Avg__c= (sumOfChilds.get(clientRollUpupdate[k].id)/(studentTeachers.size()));
            }
            
            if(maxOfChilds.containsKey(clientRollUpupdate[k].id)){
                
                clientRollUpupdate[k].Max__c= maxOfChilds.get(clientRollUpupdate[k].id);
            }
            
            
            if(minOfChilds.containsKey(clientRollUpupdate[k].id)){
                
                clientRollUpupdate[k].Min__c= minOfChilds.get(clientRollUpupdate[k].id);
            }
            
            
            allClientCountupdates.add(clientRollUpupdate[k]);
        }   
       
       update  allClientCountupdates;
    
    }
}