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

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 trigger on the same object and on different events. For this we don’t have to write 4 different trigger. With a Helper class it can be done in one class.

Suppose you have 4 trigger 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!

Rajeev Kumar

Rajeev has over 20 years of experience in IT managing large software development programs, organization change initiatives and Agile transformations. Prior to joining Mirketa Rajeev worked at startups and fortune 500 companies in senior management roles. He founded www.mytafi.in and www.finacast.com, personal finance management applications for Indian and US market. Rajeev is also the founder of www.wednik.com, a wedding planning application with online market for wedding planners and wedding merchandise.
Posted in Apex Development, Salesforce.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

*