Salesforce Developer Guide-Use of Helper/Handler class to Manage Trigger Execution
Author
January 18, 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!
Pranshu Goyal, Director of Products at Mirekta, states: “We envision DSM to be used by every small to a medium-sized organization dealing with bad data and want to get rid of duplicates easily with no cost. We have faced issues dealing with duplicates in our organization. That inspired us to make a solution that is not only simple to use but can be used widely to make the organization’s data clean to make them more efficient and productive. We want DSM to be a solution for every organization looking for duplicate management capability better than the Salesforce out-of-the-box solution with no additional cost.”
Recent Posts
-
The Hybrid Advantage in Salesforce QA20 May 2026 Blog -
CI/CD in Salesforce: Role of QA in DevOps Pipeline19 May 2026 Blog -
Reasons Why Addiction Treatment Centers should consider leveraging Salesforce as a CRM18 May 2026 Blog -
Insights on employee engagement15 May 2026 Blog -
AI Governance Why Businesses Need an AI Strategy Before Implementing AI14 May 2026 Blog -
Salesforce Agentforce: Revolutionizing AI with Autonomous Agents13 May 2026 Blog -
Salesforce Headless 360 Guide | Mirketa13 May 2026 Blog -
Salesforce Custom Permissions12 May 2026 Blog -
Salesforce Education Cloud – Time is Now11 May 2026 Blog -
Top Salesforce Performance Optimization Techniques for Large Enterprises08 May 2026 Blog