-
Notifications
You must be signed in to change notification settings - Fork 461
TriggerHandler
virtual
An opinionated trigger handler framework. Originally by Kevin O'Hara github.com/kevinohara80/sfdc-trigger-framework
Group Shared Code
See PlatformEventRecipesTriggerHandler
private static loopCountMap
Map<String,LoopCount>
private static bypassedHandlers
Set<String>
TESTVISIBLE
protected context
TriggerContext
TESTVISIBLE
private isTriggerExecuting
Boolean
Constructs a trigger handler object and ensures the context is set
public TriggerHandler()
This is main brokering method that is called by the trigger. It's responsible for determining the proper context, and calling the correct method
public virtual void run()
void
AccountTriggerHandler.run();
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);
Allows developers to turn off the max loop count
public void clearMaxLoopCount()
void
In the context of a TriggerHandler
class:
this.clearMaxLoopCount();
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');
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');
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');
removes all classes from the bypass list
public static void clearAllBypasses()
void
Triggerhandler.clearAllBypasses();
TESTVISIBLE
private instancemethods
private void setTriggerContext()
void
TESTVISIBLE
Internal method for manually setting the trigger context
private void setTriggerContext(String ctx, Boolean testMode)
Name | Type | Description |
---|---|---|
ctx | String | The current trigger Context |
testMode | Boolean | Is the trigger running in a test context? |
void
TESTVISIBLE
increment the loop count
protected void addToLoopCount()
void
Throws: loop count exception if the max loop count is reached
TESTVISIBLE
make sure this trigger should continue to run
protected Boolean validateRun()
Boolean
TriggerHandlerException: thrown when executing outside of a,[object Object],trigger
TESTVISIBLE
Returns the string version of the handler class being invoked
private String getHandlerName()
String
Name of the Handler
TESTVISIBLE
SUPPRESSWARNINGS
context methods
protected virtual void beforeInsert()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeUpdate()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeDelete()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterInsert()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUpdate()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterDelete()
void
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUndelete()
void
Internal TriggerHandler custom exception class
TESTVISIBLE
inner classes
private max
Integer
private count
Integer
Loop counter method with default of 5.
public LoopCount()
Sets loop count based on the param.
public LoopCount(Integer max)
Name | Type | Description |
---|---|---|
max | Integer | Maximum number of loops to allow. |
Increment the internal counter returning the results of this.exceeded().
public Boolean increment()
Boolean
true if count will exceed max count or is less,[object Object],than 0.
Determines if this we're about to exceed the loop count.
public Boolean exceeded()
Boolean
true if less than 0 or more than max.
Returns the max loop count.
public Integer getMax()
Integer
max loop count.
Returns the current loop count.
public Integer getCount()
Integer
current loop count.
Sets the max loop size
public void setMax(Integer max)
Name | Type | Description |
---|---|---|
max | Integer | The integer to set max to. |
void
TESTVISIBLE
possible trigger contexts
Value | Description |
---|---|
BEFORE_INSERT | |
BEFORE_UPDATE | |
BEFORE_DELETE | |
AFTER_INSERT | |
AFTER_UPDATE | |
AFTER_DELETE | |
AFTER_UNDELETE |