Skip to content

TriggerHandler

pozil edited this page Nov 14, 2024 · 21 revisions

TriggerHandler Class

virtual

An opinionated trigger handler framework. Originally by Kevin O'Hara github.com/kevinohara80/sfdc-trigger-framework

Group Shared Code

See AccountTriggerHandler

See PlatformEventRecipesTriggerHandler

Fields

loopCountMap

Signature

private static loopCountMap

Type

Map<String,LoopCount>


bypassedHandlers

Signature

private static bypassedHandlers

Type

Set<String>


context

TESTVISIBLE

Signature

protected context

Type

TriggerContext


isTriggerExecuting

TESTVISIBLE

Signature

private isTriggerExecuting

Type

Boolean

Constructors

TriggerHandler()

Constructs a trigger handler object and ensures the context is set

Signature

public TriggerHandler()

Methods

run()

This is main brokering method that is called by the trigger. It's responsible for determining the proper context, and calling the correct method

Signature

public virtual void run()

Return Type

void

Example

AccountTriggerHandler.run();

setMaxLoopCount(max)

Allows developers to prevent trigger loops, or allow a limited number of them by setting the maximum number of times this trigger is called.

Signature

public void setMaxLoopCount(Integer max)

Parameters

Name Type Description
max Integer A valid number (generally 1) of times you'd like
to allow the trigger to run.

Return Type

void

Example

In the context of a TriggerHandler class:

this.setMaxLoopCount(5);

clearMaxLoopCount()

Allows developers to turn off the max loop count

Signature

public void clearMaxLoopCount()

Return Type

void

Example

In the context of a TriggerHandler class:

this.clearMaxLoopCount();

bypass(handlerName)

Allows developers to conditionally bypass (disable) other triggers that also implement this triggerHandler

Signature

public static void bypass(String handlerName)

Parameters

Name Type Description
handlerName String Class name (String) of the trigger handler to bypass

Return Type

void

Example

TriggerHandler.bypass('AccountTriggerHandler');

clearBypass(handlerName)

Removes a given trigger handler class name from the list of bypassed trigger handlers.

Signature

public static void clearBypass(String handlerName)

Parameters

Name Type Description
handlerName String Handler class name to remove from the bypass list

Return Type

void

Example

TriggerHandler.clearBypass('AccountTriggerHandler');

isBypassed(handlerName)

Allows developers to check whether a given trigger handler class is currently bypassed.

Signature

public static Boolean isBypassed(String handlerName)

Parameters

Name Type Description
handlerName String The name of the trigger handler class to check for

Return Type

Boolean

Example

TriggerHandler.isBypassed('AccountTriggerHandler');

clearAllBypasses()

removes all classes from the bypass list

Signature

public static void clearAllBypasses()

Return Type

void

Example

Triggerhandler.clearAllBypasses();

setTriggerContext()

TESTVISIBLE

private instancemethods

Signature

private void setTriggerContext()

Return Type

void


setTriggerContext(ctx, testMode)

TESTVISIBLE

Internal method for manually setting the trigger context

Signature

private void setTriggerContext(String ctx, Boolean testMode)

Parameters

Name Type Description
ctx String The current trigger Context
testMode Boolean Is the trigger running in a test context?

Return Type

void


addToLoopCount()

TESTVISIBLE

increment the loop count

Signature

protected void addToLoopCount()

Return Type

void

Throws

Throws: loop count exception if the max loop count is reached


validateRun()

TESTVISIBLE

make sure this trigger should continue to run

Signature

protected Boolean validateRun()

Return Type

Boolean

Throws

TriggerHandlerException: thrown when executing outside of a,[object Object],trigger


getHandlerName()

TESTVISIBLE

Returns the string version of the handler class being invoked

Signature

private String getHandlerName()

Return Type

String

Name of the Handler


beforeInsert()

TESTVISIBLE SUPPRESSWARNINGS

context methods

Signature

protected virtual void beforeInsert()

Return Type

void


beforeUpdate()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void beforeUpdate()

Return Type

void


beforeDelete()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void beforeDelete()

Return Type

void


afterInsert()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterInsert()

Return Type

void


afterUpdate()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterUpdate()

Return Type

void


afterDelete()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterDelete()

Return Type

void


afterUndelete()

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterUndelete()

Return Type

void

Classes

TriggerHandlerException Class

Internal TriggerHandler custom exception class

LoopCount Class

TESTVISIBLE

inner classes

Fields

max
Signature
private max
Type

Integer


count
Signature
private count
Type

Integer

Constructors

LoopCount()

Loop counter method with default of 5.

Signature
public LoopCount()

LoopCount(max)

Sets loop count based on the param.

Signature
public LoopCount(Integer max)
Parameters
Name Type Description
max Integer Maximum number of loops to allow.

Methods

increment()

Increment the internal counter returning the results of this.exceeded().

Signature
public Boolean increment()
Return Type

Boolean

true if count will exceed max count or is less,[object Object],than 0.


exceeded()

Determines if this we're about to exceed the loop count.

Signature
public Boolean exceeded()
Return Type

Boolean

true if less than 0 or more than max.


getMax()

Returns the max loop count.

Signature
public Integer getMax()
Return Type

Integer

max loop count.


getCount()

Returns the current loop count.

Signature
public Integer getCount()
Return Type

Integer

current loop count.


setMax(max)

Sets the max loop size

Signature
public void setMax(Integer max)
Parameters
Name Type Description
max Integer The integer to set max to.
Return Type

void

Enums

TriggerContext Enum

TESTVISIBLE

possible trigger contexts

Values

Value Description
BEFORE_INSERT
BEFORE_UPDATE
BEFORE_DELETE
AFTER_INSERT
AFTER_UPDATE
AFTER_DELETE
AFTER_UNDELETE
Clone this wiki locally