As a best practice Object should have only one trigger and triggers should be logicless. Putting logic into your triggers creates un-testable, difficult-to-maintain code. It's widely accepted that a best-practice is to move trigger logic into a handler class.
This trigger framework bundles a single TriggerHandler base class that you can inherit from in all of your trigger handlers. The base class includes context-specific methods that are automatically called when a trigger is executed.
The base class also provides a secondary role as a supervisor for Trigger execution. It acts like a watchdog, monitoring trigger activity and providing an api for controlling certain aspects of execution and control flow.
You can controll your triggers from metadata records like activate/deactivate and turn on/off for specific trigger action
But the most important part of this framework is that it's minimal and simple to use.
To create a trigger handler, you simply need to create a class that inherits from TriggerHandler.cls. Here is an example for creating an Account trigger handler.
public class AccountTriggerHandler extends TriggerHandler {
In your trigger handler, to add logic to any of the trigger contexts, you only need to override them in your trigger handler. Here is how we would add logic to a beforeUpdate
trigger.
public class AccountTriggerHandler extends TriggerHandler {
public override void beforeUpdate() {
for(Account account : (List<Account>) Trigger.new) {
// do something
}
}
// add overrides for other contexts
}
Note: When referencing the Trigger statics within a class, SObjects are returned versus SObject subclasses like Opportunity, Account, etc. This means that you must cast when you reference them in your trigger handler. You could do this in your constructor if you wanted.
public class AccountTriggerHandler extends TriggerHandler {
private Map<Id, Account> newOppMap;
public AccountTriggerHandler() {
this.newOppMap = (Map<Id, Account>) Trigger.newMap;
}
public override void afterUpdate() {
//
}
}
After creating handler class you need to create Tirgger_Handler__mdt record for that handler with object name and trigger actions for which you want to execute your trigger
To use the trigger handler, you need to construct an instance of TtriggerHandler and call the run()
method. Here is an example of the Account trigger.
trigger AccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
new TriggerHandler().run();
}
To prevent recursion, you can set a max loop count for Trigger Handler. If this max is exceeded, and exception will be thrown. A great use case is when you want to ensure that your trigger runs once and only once within a single execution. Example:
public class AccountTriggerHandler extends TriggerHandler {
public AccountTriggerHandler() {
this.setMaxLoopCount(1);
}
public override void afterUpdate() {
List<Account> opps = [SELECT Id FROM Account WHERE Id IN :Trigger.newMap.keySet()];
update opps; // this will throw after this update
}
}
What if you want to tell other trigger handlers to halt execution? That's easy with the bypass api:
public class OpportunityTriggerHandler extends TriggerHandler {
public override void afterUpdate() {
List<Opportunity> opps = [SELECT Id, AccountId FROM Opportunity WHERE Id IN :Trigger.newMap.keySet()];
Account acc = [SELECT Id, Name FROM Account WHERE Id = :opps.get(0).AccountId];
TriggerHandler.bypass('AccountTriggerHandler');
acc.Name = 'No Trigger';
update acc; // won't invoke the AccountTriggerHandler
TriggerHandler.clearBypass('AccountTriggerHandler');
acc.Name = 'With Trigger';
update acc; // will invoke the AccountTriggerHandler
}
}
use below code if you want to bypass only specific context
// bypass
TriggerHandler.bypassContext('AccountTriggerHandler', TriggerHandler.TriggerContext.AFTER_UPDATE);
// clear context
TriggerHandler.clearBypassContext('AccountTriggerHandler',TriggerHandler.TriggerContext.AFTER_UPDATE);
If you need to check if a handler is bypassed, use the isBypassed
and isBypassedContext
method:
if (TriggerHandler.isBypassed('AccountTriggerHandler')) {
// ... do something if the Account trigger handler is bypassed!
}
if (TriggerHandler.isBypassedContext('AccountTriggerHandler', TriggerHandler.TriggerContext.AFTER_UPDATE)) {
// ... do something if the Account trigger handler is bypassed!
}
If you want to clear all bypasses for the transaction, simple use the clearAllBypasses
method, as in:
// ... done with bypasses!
TriggerHandler.clearAllBypasses();
// ... now handlers won't be ignored!
Avaliable trigger Context
TriggerHandler.TriggerContext.BEFORE_INSERT
TriggerHandler.TriggerContext.BEFORE_UPDATE
TriggerHandler.TriggerContext.BEFORE_DELETE
TriggerHandler.TriggerContext.AFTER_INSERT
TriggerHandler.TriggerContext.AFTER_UPDATE
TriggerHandler.TriggerContext.AFTER_DELETE
TriggerHandler.TriggerContext.AFTER_UNDELETE
Here are all of the methods that you can override. All of the context possibilities are supported.
beforeInsert()
beforeUpdate()
beforeDelete()
afterInsert()
afterUpdate()
afterDelete()
afterUndelete()