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
-
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
-
The Ultimate Guide to NetSuite Development: Tools and Techniques10 Jan 2025 Blog
-
How Salesforce Nonprofit Cloud Transforms Fundraising Strategies10 Jan 2025 Blog
-
The Impact of Salesforce Development Partners on Small and Medium Businesses08 Jan 2025 Blog
-
Key Questions to Ask When Hiring a NetSuite Development Partner08 Jan 2025 Blog
-
Salesforce Agentforce Demystified: Your Essential Guide08 Jan 2025 Blog
-
Salesforce and NetSuite Integration: Driving Business Efficiency with Precision06 Jan 2025 Blog
-
Everest Group has positioned Mirketa as an Aspirant in the report24 Dec 2024 Press Release
-
Salesforce Einstein20 Dec 2024 E-Book
Categories
Featured by












Office Locations

11501 Dublin Blvd STE 200, Dublin, CA 94568, USA

B-4/5 First Floor, Sector- 63, Noida 201301, India
