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
Lakshmi SLakshmi S 

How to reduce(bulkify) this code using Map ?

Hi Team,

How to reduce (bulkify) code using Map?

Trigger
---------------
trigger FinancialsRollupSummary on Financials__c (after insert, after update, after delete, after undelete) {
    
    // If condition for After insert and undelete records.
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUndelete)){
       
        // Calling Handler class Insert method.
        FinancialsRollupSummaryHandlerCls.insertFinancials((List<Financials__c>)Trigger.New);
    }
    
    // If condition for After delete records.
    if(Trigger.isAfter && Trigger.isDelete){
        
        // Calling Handler class Delete method.
        FinancialsRollupSummaryHandlerCls.deleteFinancials((List<Financials__c>)Trigger.Old);
    }
    
    // If condition for After Update records.
    if(Trigger.isAfter && Trigger.isUpdate){
        
        // Calling Handler class Update method.
        FinancialsRollupSummaryHandlerCls.updateFinancials((List<Financials__c>)Trigger.old);
    }

}
Trigger Handler Class
--------------------------------
public class FinancialsRollupSummaryHandlerCls {
    
    Public static String recordYear = 'FY-2016';
    Public static Decimal grsum ;
    Public static Decimal gesum ;
    Public static Decimal amsgrsum ;
    Public static Decimal amsgesum ;
    
    // Static method for insert Financial records
    public static void insertFinancials(List<Financials__c> newrecs){
        
        List<Account> updateAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
        for(Financials__c fin : newrecs){
            
            if(fin.AccountName__c != null){
                accIds.add(fin.AccountName__c);
            }
        }
        
        if(accIds.size() > 0){
            
            for(Account acc : [Select id,name,RS_Global_EBITDA_Sum__c,RS_Global_Revenue_Sum__c,RS_AMS_Global_Revenue_Sum__c,RS_AMS_Global_EBITDA_B4_Sum__c,
                              (Select id,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c,AMS_Global_Rev__c,AMS_Global_EBITDA_B4__c,Year__c from Financial__r) 
                               from Account where Id In :accIds]){
                                   
                                   for(AggregateResult ag : [Select sum(Global_Revenue_Total__c)rev,sum(Global_EBITDA_B4_Total__c)ebsum,sum(AMS_Global_Rev__c)amsrev,sum(AMS_Global_EBITDA_B4__c)amsgesum from Financials__c where AccountName__c =: acc.id and Year__c=: recordYear]){
                                                                 
                                                                 grsum = (Decimal)ag.get('rev');
                                                                 gesum = (Decimal)ag.get('ebsum');
                                                                 amsgrsum = (Decimal)ag.get('amsrev');
                                                                 amsgesum = (Decimal)ag.get('amsgesum');
                                                             }
                                   acc.RS_Global_Revenue_Sum__c = grsum;
                                   acc.RS_Global_EBITDA_Sum__c = gesum;
                                   acc.RS_AMS_Global_Revenue_Sum__c = amsgrsum;
                                   acc.RS_AMS_Global_EBITDA_B4_Sum__c = amsgesum;
                                   
                                   
                                   updateAccs.add(acc);
                               }
            update updateAccs;
        }
    
    }
    
    // Static method for delete financial records
    public static void deleteFinancials(List<Financials__c> delrecs){
        
               
        List<Account> updateAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
        for(Financials__c fin : delrecs){
            
            if(fin.AccountName__c != null){
                accIds.add(fin.AccountName__c);
            }
        }
        
        if(accIds.size() > 0){
            
            for(Account acc : [Select id,name,RS_Global_EBITDA_Sum__c,RS_Global_Revenue_Sum__c,RS_AMS_Global_Revenue_Sum__c,RS_AMS_Global_EBITDA_B4_Sum__c,
                              (Select id,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c,AMS_Global_Rev__c,AMS_Global_EBITDA_B4__c,Year__c from Financial__r) 
                               from Account where Id In :accIds]){
                                   
                                   for(AggregateResult ag : [Select sum(Global_Revenue_Total__c)rev,sum(Global_EBITDA_B4_Total__c)ebsum,sum(AMS_Global_Rev__c)amsrev,sum(AMS_Global_EBITDA_B4__c)amsgesum from Financials__c where AccountName__c =: acc.id and Year__c=: recordYear]){
                                                                 
                                                                 grsum = (Decimal)ag.get('rev');
                                                                 gesum = (Decimal)ag.get('ebsum');
                                                                 amsgrsum = (Decimal)ag.get('amsrev');
                                                                 amsgesum = (Decimal)ag.get('amsgesum');
                                                             }
                                   acc.RS_Global_Revenue_Sum__c = grsum;
                                   acc.RS_Global_EBITDA_Sum__c = gesum;
                                   acc.RS_AMS_Global_Revenue_Sum__c = amsgrsum;
                                   acc.RS_AMS_Global_EBITDA_B4_Sum__c = amsgesum;
                                   
                                   
                                   updateAccs.add(acc);
                               }
            update updateAccs;
        }
    
    }
    
    // Static method for update Financial records.
    public static void updateFinancials(List<Financials__c> updaterecs){
        
               
        List<Account> updateAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
        for(Financials__c fin : updaterecs){
            
            if(fin.AccountName__c != null){
                accIds.add(fin.AccountName__c);
            }
        }
        
        if(accIds.size() > 0){
            
            for(Account acc : [Select id,name,RS_Global_EBITDA_Sum__c,RS_Global_Revenue_Sum__c,RS_AMS_Global_Revenue_Sum__c,RS_AMS_Global_EBITDA_B4_Sum__c,
                              (Select id,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c,AMS_Global_Rev__c,AMS_Global_EBITDA_B4__c,Year__c from Financial__r) 
                               from Account where Id In :accIds]){
                                   
                                   for(AggregateResult ag : [Select sum(Global_Revenue_Total__c)rev,sum(Global_EBITDA_B4_Total__c)ebsum,sum(AMS_Global_Rev__c)amsrev,sum(AMS_Global_EBITDA_B4__c)amsgesum from Financials__c where AccountName__c =: acc.id and Year__c=: recordYear]){
                                                                 
                                                                 grsum = (Decimal)ag.get('rev');
                                                                 gesum = (Decimal)ag.get('ebsum');
                                                                 amsgrsum = (Decimal)ag.get('amsrev');
                                                                 amsgesum = (Decimal)ag.get('amsgesum');
                                                             }
                                   acc.RS_Global_Revenue_Sum__c = grsum;
                                   acc.RS_Global_EBITDA_Sum__c = gesum;
                                   acc.RS_AMS_Global_Revenue_Sum__c = amsgrsum;
                                   acc.RS_AMS_Global_EBITDA_B4_Sum__c = amsgesum;
                                   
                                   
                                   updateAccs.add(acc);
                               }
            update updateAccs;
        }
    
    }
Can anyone let me know how to do this using Map?

Regards
Lakshmi

 
Ashif KhanAshif Khan
Hi
I work on insertFinancials method same concept you can use in other methods
 
public static void insertFinancials(List<Financials__c> newrecs){
        
        List<Account> updateAccs = new List<Account>();
        Set<Id> accIds = new Set<Id>();
        
        for(Financials__c fin : newrecs){
            
            if(fin.AccountName__c != null){
                accIds.add(fin.AccountName__c);
            }
        }
        Map<Id, AggregateResult>    aggReslt= new Map<Id, AggregateResult>();
        if(accIds.size() > 0){
            for(AggregateResult agg : [Select AccountName__c,sum(Global_Revenue_Total__c)rev,sum(Global_EBITDA_B4_Total__c)ebsum,sum(AMS_Global_Rev__c)amsrev,sum(AMS_Global_EBITDA_B4__c)amsgesum from Financials__c where AccountName__c In :accIds and Year__c=: recordYear]){
                
                aggReslt.put(ag.AccountName__c,agg);                         
                
            }
       
            for(Account acc : [Select id,name,RS_Global_EBITDA_Sum__c,RS_Global_Revenue_Sum__c,RS_AMS_Global_Revenue_Sum__c,RS_AMS_Global_EBITDA_B4_Sum__c,
                               (Select id,Global_Revenue_Total__c,Global_EBITDA_B4_Total__c,AMS_Global_Rev__c,AMS_Global_EBITDA_B4__c,Year__c from Financial__r) 
                               from Account where Id In :accIds]){
                                   
                                    AggregateResult ag=aggReslt.get(Id);
                                   acc.RS_Global_Revenue_Sum__c = (Decimal)ag.get('rev');
                                   acc.RS_Global_EBITDA_Sum__c = (Decimal)ag.get('ebsum');
                                   acc.RS_AMS_Global_Revenue_Sum__c = (Decimal)ag.get('amsrev');
                                   acc.RS_AMS_Global_EBITDA_B4_Sum__c = (Decimal)ag.get('amsgesum');
                                   
                                   
                                   updateAccs.add(acc);
                               }
            update updateAccs;
        }
        
    }

Regards 
Ashif
Ashif KhanAshif Khan
Is it working for you? Then close the question.