Manage Order of Trigger Execution in Salesforce for Object
Hi guys,
Note: Handle the trigger order of execution:
If any Case when you required to write 4 triggers on different - different events but on same object
then don't write 4 trigger for same.
Suppose you have 4 trigger on Account Object.
1- AccTrigger1.trigger
2- AccTrigger2.trigger
3- AccTrigger3.trigger
4- AccTrigger4.trigger
Solution is mention as below
STEP 1- Create only one trigger for one object(Account) like- In above case instead of having 5 triggers on
STEP 2- Create only 1 trigger that is AccountTrigger.trigger on Account Object.
// Create Trigger as Mention Below
trigger AccountTrigger on Account (after insert, after update, before delete, before insert, before update)
{
AccortunityTriggerHandler triggerHandler = new AccortunityTriggerHandler(Trigger.isExecuting,Trigger.size);
if(Trigger.isBefore && Trigger.isInsert){
triggerHandler.OnBeforeInsert(trigger.new, trigger.NewMap);
}
if(Trigger.isAfter && Trigger.isInsert){
triggerHandler.OnAfterInsert(trigger.new, trigger.NewMap);
}
if(Trigger.isBefore && Trigger.isUpdate){
triggerHandler.OnBeforeUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
}
if(Trigger.isAfter && Trigger.isUpdate){
triggerHandler.OnAfterUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
}
if(Trigger.isBefore && Trigger.isDelete){
triggerHandler.OnBeforeDelete(trigger.Old, Trigger.oldMap);
}
if(Trigger.isAfter && Trigger.isDelete){
triggerHandler.OnAfterDelete(trigger.Old, Trigger.oldMap);
}
}
// end of trigger
// Create a new Class and handle all events in the class with creating method
STEP 3- In the trigger I have called different Methods for Different DML Event and these methods are declared in a class "AccortunityTriggerHandler.cls". the code of this class is like-
public class AccortunityTriggerHandler {
private boolean m_isExecuting = True;
private integer BatchSize = 0;
private final static boolean ISUPDATE_TRUE = true;
private final static boolean ISUPDATE_FALSE = false;
public AccortunityTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
public void onBeforeInsert(list<Account> triggerNew, map<id,Account> triggerNewMap)
{
// call Before Insert Method
}
public void OnAfterInsert(list<Account> triggerNew, map<id,Account> triggerNewMap)
{
// call After Insert Method
//MehtodNameAfterInsert
}
public void OnBeforeUpdate(list<Account> triggerNew, map<id,Account> triggerNewMap, map<id,Account> triggerOldMap)
{
// call Before update Method
}
public void OnAfterUpdate(list<Account> triggerNew, map<id,Account> triggerNewMap, map<id,Account> triggerOldMap)
{
// call After update Method
}
public void OnBeforeDelete(list<Account> triggerOld, map<id,Account> triggerOldMap)
{
// call Before Delete Method
}
public void OnAfterDelete(list<Account> triggerOld, map<id,Account> triggerOldMap)
{
// call After Delete Method
}
private void MehtodNameAfterInsert(list<Account> triggerNew,map<id,Account> triggerNewMap)
{
// write logic here
}
// create more method as per your requirement
}
Thanks
Sumit Shukla
sumit@cccinfotech.com
cccinfotech.com
9711055997
Note: Handle the trigger order of execution:
If any Case when you required to write 4 triggers on different - different events but on same object
then don't write 4 trigger for same.
Suppose you have 4 trigger on Account Object.
1- AccTrigger1.trigger
2- AccTrigger2.trigger
3- AccTrigger3.trigger
4- AccTrigger4.trigger
Solution is mention as below
STEP 1- Create only one trigger for one object(Account) like- In above case instead of having 5 triggers on
STEP 2- Create only 1 trigger that is AccountTrigger.trigger on Account Object.
// Create Trigger as Mention Below
trigger AccountTrigger on Account (after insert, after update, before delete, before insert, before update)
{
AccortunityTriggerHandler triggerHandler = new AccortunityTriggerHandler(Trigger.isExecuting,Trigger.size);
if(Trigger.isBefore && Trigger.isInsert){
triggerHandler.OnBeforeInsert(trigger.new, trigger.NewMap);
}
if(Trigger.isAfter && Trigger.isInsert){
triggerHandler.OnAfterInsert(trigger.new, trigger.NewMap);
}
if(Trigger.isBefore && Trigger.isUpdate){
triggerHandler.OnBeforeUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
}
if(Trigger.isAfter && Trigger.isUpdate){
triggerHandler.OnAfterUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
}
if(Trigger.isBefore && Trigger.isDelete){
triggerHandler.OnBeforeDelete(trigger.Old, Trigger.oldMap);
}
if(Trigger.isAfter && Trigger.isDelete){
triggerHandler.OnAfterDelete(trigger.Old, Trigger.oldMap);
}
}
// end of trigger
// Create a new Class and handle all events in the class with creating method
STEP 3- In the trigger I have called different Methods for Different DML Event and these methods are declared in a class "AccortunityTriggerHandler.cls". the code of this class is like-
public class AccortunityTriggerHandler {
private boolean m_isExecuting = True;
private integer BatchSize = 0;
private final static boolean ISUPDATE_TRUE = true;
private final static boolean ISUPDATE_FALSE = false;
public AccortunityTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
public void onBeforeInsert(list<Account> triggerNew, map<id,Account> triggerNewMap)
{
// call Before Insert Method
}
public void OnAfterInsert(list<Account> triggerNew, map<id,Account> triggerNewMap)
{
// call After Insert Method
//MehtodNameAfterInsert
}
public void OnBeforeUpdate(list<Account> triggerNew, map<id,Account> triggerNewMap, map<id,Account> triggerOldMap)
{
// call Before update Method
}
public void OnAfterUpdate(list<Account> triggerNew, map<id,Account> triggerNewMap, map<id,Account> triggerOldMap)
{
// call After update Method
}
public void OnBeforeDelete(list<Account> triggerOld, map<id,Account> triggerOldMap)
{
// call Before Delete Method
}
public void OnAfterDelete(list<Account> triggerOld, map<id,Account> triggerOldMap)
{
// call After Delete Method
}
private void MehtodNameAfterInsert(list<Account> triggerNew,map<id,Account> triggerNewMap)
{
// write logic here
}
// create more method as per your requirement
}
Thanks
Sumit Shukla
sumit@cccinfotech.com
cccinfotech.com
9711055997
Comments
Post a Comment