You need to sign in to do that
Don't have an account?

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 :
---------------
I want to bulkify this code using trigger handler class.
Regards,
Lakshmi
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
If you want to pass map as argument in the method than you can do it via below two ways.
First Way:
Second Way:
Thanks
Preyanka
All Answers
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:
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
Hi,
From trigger you can send old and new value map to your handler methods.
Example:
Your trigger will be
Your class will be:
Hope that helps. Put API name of the fields that you want to compare.
Note: This is just a sample code.
Regards,
Paras 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:
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
If you want to pass map as argument in the method than you can do it via below two ways.
First Way:
Second Way:
Thanks
Preyanka
Could you please mark it solved/best answer if it answer your query.