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 

Bulkify the before insert or update Triggers?

Hi Team,

How can we bulkify this using trigger handler class, please let me know any one.
Trigger :
---------------
trigger OpportunityTrig on Opportunity (before insert, before update, after insert, after update) {
    
    
    if(Trigger.isBefore && Trigger.isInsert){
        
  for(opportunity op : Trigger.New){
            //opportunity o = Trigger.oldmap.get(op.Id);
            if(op.Close_Deal_Date__c != null){
                
                
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null){
                op.Reporting_Quarter__c = null;
            }
            
            
        }

    }
    
    if(Trigger.isBefore && Trigger.isUpdate){
       OpportunityTriggerHandlerCls.oppCategoryUpdate((List<Opportunity>)Trigger.New);
        for(opportunity op : Trigger.New){
            opportunity o = Trigger.oldmap.get(op.Id);
            if(op.Close_Deal_Date__c != null && (op.Close_Deal_Date__c != o.Close_Deal_Date__c)){
                
                
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null && (op.Close_Deal_Date__c != o.Close_Deal_Date__c)){
                op.Reporting_Quarter__c = null;
            }
            
        }
    }
}

I want to bulkify this code using trigger handler class.


Regards,
Lakshmi
 
Best Answer chosen by Lakshmi S
PreyankaPreyanka
Hi Lakshmi,

If you want to pass map as argument in the method than you can do it via below two ways. 

First Way:
Public static void reportingQuarterUpdate2(Map<Id, Opportunity> newMapRec, Map<Id, Opportunity> oldMapRec){
		List<opportunity> newRec = newMapRec.values;
		List<opportunity> oldRec = oldMapRec.values;
		
       for(opportunity op : newRec){
           for(opportunity op2 : oldRec){
            if(op.Close_Deal_Date__c != null && (op.Close_Deal_Date__c != op2.Close_Deal_Date__c)){
                
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null && (op.Close_Deal_Date__c != op2.Close_Deal_Date__c)){
                op.Reporting_Quarter__c = null;
            }
            }
        }
    }

Second Way:
Public static void reportingQuarterUpdate2(Map<Id, Opportunity> newMapRec, Map<Id, Opportunity> oldMapRec){
        for(ID oppId : newMapRec.keySet()){
            Opportunity op = newMapRec.get(oppId);			
            if(op.Close_Deal_Date__c != null && (op.Close_Deal_Date__c != oldMapRec.get(oppId).Close_Deal_Date__c)){
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null && (op.Close_Deal_Date__c != oldMapRec.get(oppId).Close_Deal_Date__c)){
                op.Reporting_Quarter__c = null;
            }
            
        }
    }

Thanks
Preyanka​​

All Answers

Paras_BhattParas_Bhatt

What Issue You are facing while bulkifying the code?

 

You simple need to create one or more methods in your handler class and put your code in it.

 

For Example:

 

for(opportunity op : Trigger.New){
            //opportunity o = Trigger.oldmap.get(op.Id);
            if(op.Close_Deal_Date__c != null){
                
                
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null){
                op.Reporting_Quarter__c = null;
            }
            
            
        }


This block can be moved to you handler, simply create method that will take list of opportunities and just pass your tigger.new instance to it.

Let me know in case you face any issue.
 

Regards,

Paras Bhatt

Paras_BhattParas_Bhatt

Hi,

From trigger you can send old and new value map to your handler methods.

Example:

Your trigger will be

if(Trigger.isBefore){
        if(Trigger.isUpdate ){
            OpportunityTriggerHandler.handleBulkUpload(Trigger.newMap,Trigger.oldMap);    
        }   
   }

Your class will be:

public static void handleBulkUpload(map<id,Opportunity> newMap,map<id,Opportunity> oldMap){
		
		//Assume you need to update certain records based on the condition
		list<Opportunity> optyListToUpdate = new list<Opportunity>();
        
		for(String tempOptyKey: newMap.keySet() ){
			// Comapre the values
            if(newMap.get(tempOptyKey).<Your Field>!= oldMap.get(tempOptyKey).<Your Field>){
				//Add to the list which will be updated 
				Opportunity tempOpty = newMap.get(tempOptyKey);
				//update any field value if required
				optyListToUpdate.add(tempOpty);
            }
        } 
		
		//updating the list of the loop 
		if(!optyListToUpdate.isEmpty()){
			update optyListToUpdate;
		}
    }
 


Hope that helps. Put API name of the fields that you want to compare. 

Note: This is just a sample code.

Regards,

Paras Bhatt

Paras_BhattParas_Bhatt

Note: If your trigger is before Insert/Update and you need to set/update value on same opportunity record.

You don't need to make additional Update Call, 

Example:

public static void handleBulkUpload(map<id,Opportunity> newMap,map<id,Opportunity> oldMap){
		
			for(String tempOptyKey: newMap.keySet() ){
			// Comapre the values
            if(newMap.get(tempOptyKey).<Your Field>!= oldMap.get(tempOptyKey).<Your Field>){
				//Add to the list which will be updated 
				Opportunity tempOpty = newMap.get(tempOptyKey);
				//update any field value if required, this will be automatically saved to data base. No need to make Update call.
				tempOpty.<Your Field> ='Some Value';
            }
        } 
		
	
    }
PreyankaPreyanka
Hi Lakshmi,

You can pass the old and new values of Trigger as an argument to the method of handler class as below

Suppose you handler class name is OpportunityTrigHandler and the method inside the handler is opportunityTrigMethod(). Then please define the method as below two ways:
First way :
opportunityTrigMethod(List<Opportunity> lstOldOpportunity, List<Opportunity> lstNewOpportunity ) and invoke the method from Trigger as below

OpportunityTrigHandler objHandler = new OpportunityTrigHandler();
objHandler.opportunityTrigMethod(Trigger.old, Trigger.new); 

Second Way:
 opportunityTrigMethod(Map<Id, Opportunity> mapOldOpportunity, List<Id, Opportunity> mapNewOpportunity ) and invoke the method from Trigger as below

OpportunityTrigHandler objHandler = new OpportunityTrigHandler();
objHandler.opportunityTrigMethod(Trigger.oldMap, Trigger.newMap);

Thanks
Preyanka
PreyankaPreyanka
Hi Lakshmi,

If you want to pass map as argument in the method than you can do it via below two ways. 

First Way:
Public static void reportingQuarterUpdate2(Map<Id, Opportunity> newMapRec, Map<Id, Opportunity> oldMapRec){
		List<opportunity> newRec = newMapRec.values;
		List<opportunity> oldRec = oldMapRec.values;
		
       for(opportunity op : newRec){
           for(opportunity op2 : oldRec){
            if(op.Close_Deal_Date__c != null && (op.Close_Deal_Date__c != op2.Close_Deal_Date__c)){
                
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null && (op.Close_Deal_Date__c != op2.Close_Deal_Date__c)){
                op.Reporting_Quarter__c = null;
            }
            }
        }
    }

Second Way:
Public static void reportingQuarterUpdate2(Map<Id, Opportunity> newMapRec, Map<Id, Opportunity> oldMapRec){
        for(ID oppId : newMapRec.keySet()){
            Opportunity op = newMapRec.get(oppId);			
            if(op.Close_Deal_Date__c != null && (op.Close_Deal_Date__c != oldMapRec.get(oppId).Close_Deal_Date__c)){
                Integer y = op.Close_Deal_Date__c.YEAR();
                Integer m = ((op.Close_Deal_Date__c.MONTH()-1)/3)+1;
                String q = String.valueOf(y)+' Q'+String.valueOf(m);
                system.debug('y'+y+' Mon :'+m+' Quarter:'+q);
                op.Reporting_Quarter__c = q;
            }
            else if(op.Close_Deal_Date__c == null && (op.Close_Deal_Date__c != oldMapRec.get(oppId).Close_Deal_Date__c)){
                op.Reporting_Quarter__c = null;
            }
            
        }
    }

Thanks
Preyanka​​
This was selected as the best answer
PreyankaPreyanka
Hello Lakshmi,

Could you please mark it solved/best answer if it answer your query.