-
Notifications
You must be signed in to change notification settings - Fork 464
MetadataTriggerHandler
This class exists as a unified, trigger handler class. It uses Custom Metadata, and introspection of the Trigger.new variable to determine what trigger handler classes should be called, and in what order.
Metadata_Driven_Trigger__mdt has three fields:
- Object__c - is a metadata entity look up to an sObject ie: Account
- Execution_Order__c - is an integer and determines the order the trigger
- handlers are executed
- Class__c - is a String holding the name of the Trigger Handler to execute
Note: This Trigger framework works like this:
An .trigger for a sObject invokes this class via: new MetadataTriggerHandler().run();
This trigger handler class extends TriggerHandler - all the trigger handler classes must extend trigger handler. Most classes will only overwrite the context methods like afterUpdate(). This class, however, overrides the run method. This class is responsible for determining which other trigger handler classes to instantiate and run.
Concrete example: AccountTrigger.trigger (in this org) - invokes this class. This class queries the custom metadata and will find (at least) one metadata record tied to Account and the metadata record's Class__c specifies AccountTriggerHandler. This class then loops over the returned metadata records, instantiating the classes specified. It then calls the appropriate context methods on those classes.
Note: The TriggerHandler framework below does not give you the ability to order, or re-arrange the trigger work of managed packages. It also does not allow you to declare the order of methods within the triggerHandler classes themselves. When using the MetadataTriggerHandler, it's better to have a high number of singularly focused trigger handler classes than a few classes with multiple methods.
Group Trigger Recipes
See TriggerHandler
Inheritance
TESTVISIBLE
private mts
TESTVISIBLE
private tryggers
List<Metadata_Driven_Trigger__mdt>
TESTVISIBLE
private activeHandler
Inherited
TESTVISIBLE
protected context
TriggerContext
Constructor used by live triggers.
public MetadataTriggerHandler()
public MetadataTriggerHandler(MetadataTriggerService mts)
Name | Type | Description |
---|---|---|
mts | MetadataTriggerService |
SUPPRESSWARNINGS
Overrides the standard Run() method, which allows this metadata based trigger handler can be an incremental update / sit beside other trigger handlers classes that are directly invoked by a trigger
public override void run()
void
Inherited
Allows developers to prevent trigger loops, or allow a limited number of them by setting the maximum number of times this trigger is called.
public void setMaxLoopCount(Integer max)
Name | Type | Description |
---|---|---|
max | Integer | A valid number (generally 1) of times you'd like |
to allow the trigger to run. |
void
In the context of a TriggerHandler
class:
this.setMaxLoopCount(5);
Inherited
Allows developers to turn off the max loop count
public void clearMaxLoopCount()
void
In the context of a TriggerHandler
class:
this.clearMaxLoopCount();
Inherited
Allows developers to conditionally bypass (disable) other triggers that also implement this triggerHandler
public static void bypass(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | Class name (String) of the trigger handler to bypass |
void
TriggerHandler.bypass('AccountTriggerHandler');
Inherited
Removes a given trigger handler class name from the list of bypassed trigger handlers.
public static void clearBypass(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | Handler class name to remove from the bypass list |
void
TriggerHandler.clearBypass('AccountTriggerHandler');
Inherited
Allows developers to check whether a given trigger handler class is currently bypassed.
public static Boolean isBypassed(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | The name of the trigger handler class to check for |
Boolean
TriggerHandler.isBypassed('AccountTriggerHandler');
Inherited
removes all classes from the bypass list
public static void clearAllBypasses()
void
Triggerhandler.clearAllBypasses();
Inherited
TESTVISIBLE
increment the loop count
protected void addToLoopCount()
void
Throws: loop count exception if the max loop count is reached
Inherited
TESTVISIBLE
make sure this trigger should continue to run
protected Boolean validateRun()
Boolean
TriggerHandlerException: thrown when executing outside of a,[object Object],trigger
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
context methods
protected virtual void beforeInsert()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeUpdate()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeDelete()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterInsert()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUpdate()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterDelete()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUndelete()
void