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
-
AI for Nonprofits: Boosting Fundraising with Salesforce Einstein, Agentforce, and Smarter InsightsShape25 Mar 2025 Use-case
-
AI-Powered Vaccination Scheduling with Einstein Copilot & Predictive AI21 Mar 2025 Use-case
-
Leveraging AI to Enhance Sales Effectiveness13 Mar 2025 Use-case
-
Revolutionizing Manufacturing with AI: Predictive Maintenance, Supply Chain Optimization, and More11 Mar 2025 E-Book
-
NetSuite for Manufacturing: Streamlining Operations and Solving Key Challenges07 Mar 2025 Blog
-
How to Build Your First Agent in Salesforce Agentforce24 Feb 2025 Blog
-
ERP vs Salesforce Revenue Cloud: Which One is Right for Your Business?24 Feb 2025 E-Book
-
Revolutionizing Manufacturing with Salesforce: A Playbook for Efficiency & Growth18 Feb 2025 E-Book
-
Salesforce 2025 Game-Changing Trends You Need to Know28 Jan 2025 Blog
-
Agentforce 2.0: Everything You Need to Know About the Latest Update22 Jan 2025 Blog
Categories
Featured by



