Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles

Blog

Salesforce Developer Guide-Use of Helper/Handler class to Manage Trigger Execution

By |2020-07-15T06:58:48+00:00January 18th, 2015|

If you are working on custom application development on force.com or customizing salesforce to do event-driven updates, you will end up writing quite a few triggers, and in some cases more than one trigger on the same object. In this blog, I will explain how to use a Helper/Handler class when we have to write more than one trigger for the same object on the different events. I will also explain how a Helper/Handler can also help manage trigger execution.

Let’s take an example where we have to write 4 triggers on the same object and on different events. For this, we don’t have to write 4 different triggers. With a Helper class, it can be done in one class.

Suppose you have 4 triggers on Account Object.

Step 1: Write an apex trigger AccountTrigger on Account with all the events
trigger AccountTrigger on Account (after insert, after undelete, after update, before delete, before insert, before update)
{
//Declaration of object objHandler.
AccountTriggerHandler objHandler = new AccountTriggerHandler();
if(trigger.isAfter && trigger.isInsert)
{
objHandler.OnAfterInsert(trigger.New, trigger.newMap);
}
if(trigger.isBefore && trigger.isInsert)
{
objHandler.OnBeforeInsert(trigger.New, trigger.newMap);
}
if(trigger.isAfter && trigger.isUpdate)
{
objHandler.OnAfterUpdate(trigger.New, trigger.newMap,trigger.Old,trigger.oldMap);
}
if(trigger.isAfter && trigger.isUpdate)
{
objHandler.OnBeforeUpdate(trigger.New, trigger.newMap,trigger.Old,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

Step 2:
Create a new Class and handle all events in the class with creating method
public class AccountTriggerHandler
{
public void OnBeforeInsert(list<Account> triggerNew,map<Id,Account> triggerNewmap)
{
// This is used to Call before Insert method.
}
public void OnAfterInsert(list<Account> triggerNew,map<Id,Account> triggerNewmap)
{
// In this method Trigger are arranged in order of its execution.
Manage1(triggerNew,triggerNewmap);
Manage2(triggerNew,triggerNewmap);
Manage3(triggerNew,triggerNewmap);
}
public void OnBeforeUpdate(list<Account> triggerNew,map<Id,Account> triggerNewmap,list<Account> triggerOld,map<Id,Account> triggerOldmap)
{
// This is used to Call before update method.
}
public void OnAfterUpdate(list<Account> triggerNew,map<Id,Account> triggerNewmap,list<Account> triggerOld,map<Id,Account> triggerOldmap)
{
// This is used to Call  After Update method.
}
private void manage1(list<Account> triggerNew,map<Id,Account> triggerNewmap)
{
// This Method is created to Show order of trigger execution.
}
private void manage2(list<Account> triggerNew,map<Id,Account> triggerNewmap)
{
// This Method is created to Show order of trigger execution.
}
private void manage3(list<Account> triggerNew,map<Id,Account> triggerNewmap)
{
// This Method is created to Show order of trigger execution.
}
}
That’s pretty much it. So next time when you have to write multiple triggers on the same object, use a Helper class to do the job.

Please feel free to reach out to me if you have any questions on this blog or on salesforce.com customization or force.com development.

Happy coding!

Leave A Comment