Skip to content

AccountTriggerHandler

pozil edited this page May 14, 2024 · 14 revisions

AccountTriggerHandler

Demonstrates how to construct a TriggerHandler using the trigger handler framework found in Shared Code/TriggerHandler.cls

Inheritance

TriggerHandler > AccountTriggerHandler

Group Trigger Recipes

See TriggerHandler

See AccountServiceLayer

Constructors

public AccountTriggerHandler()

Constructor that sets class variables based on Trigger context vars


Fields

private triggerNewList<Account>

private triggerOldList<Account>

private triggerMapNewMap<Id,Account>

private triggerMapOldMap<Id,Account>

private circuitBreakerException

TESTVISIBLE

protected contextTriggerContext

Inherited TESTVISIBLE


Methods

public override void beforeInsert()

Before Insert context method. Called automatically by the trigger framework this class extends.

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;

public override void afterInsert()

after insert context method. Called automatically by the trigger framework this class extends

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;

public override void beforeUpdate()

before update context method. Called automatically by the trigger framework this class extends

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;
accounts[0].name += ' Updated';
update accounts;

public override void afterUpdate()

SUPPRESSWARNINGS

after update context method. Called automatically by the trigger framework this class extends Note: this method contains a PMD false-positive report about CRUD checking before insert. However, trigger code is run in system mode, regardless of users, so it doesn't make sense to check for Account and Task permissions. This code is therefore only safe to execute in the context of a trigger.

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;
accounts[0].name += ' Updated';
update accounts;

public override void beforeDelete()

SUPPRESSWARNINGS

before delete context method. Called automatically by the trigger framework this class extends Note: this method contains a PMD false-positive report about CRUD checking before insert. However, trigger code is run in system mode, regardless of users, so it doesn't make sense to check for Account and Task permissions. This code is therefore only safe to execute in the context of a trigger.

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;

public override void afterDelete()

SUPPRESSWARNINGS

after delete context method. Called automatically by the trigger framework this class extends Note: this method contains a PMD false-positive report about CRUD checking before insert. However, trigger code is run in system mode, regardless of users, so it doesn't make sense to check for Account and Task permissions. This code is therefore only safe to execute in the context of a trigger.

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;

public override void afterUndelete()

SUPPRESSWARNINGS

after undelete context method. Called automatically by the trigger framework this class extends Note: this method contains a PMD false-positive report about CRUD checking before insert. However, trigger code is run in system mode, regardless of users, so it doesn't make sense to check for Account and Task permissions. This code is therefore only safe to execute in the context of a trigger.

Example

Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;
undelete accounts;

public virtual void run()

Inherited

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

Example

AccountTriggerHandler.run();

public void setMaxLoopCount(Integer max)

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.

Parameters

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

Example

In the context of a TriggerHandler class,
this.setMaxLoopCount(5);

public void clearMaxLoopCount()

Inherited

Allows developers to turn off the max loop count

Example

In the context of a TriggerHandler class,
this.clearMaxLoopCount();

public static void bypass(String handlerName)

Inherited

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

Parameters

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

Example

TriggerHandler.bypass('AccountTriggerHandler');

public static void clearBypass(String handlerName)

Inherited

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

Parameters

Param Description
handlerName Handler class name to remove from the bypass list

Example

TriggerHandler.clearBypass('AccountTriggerHandler');

public static Boolean isBypassed(String handlerName)

Inherited

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

Parameters

Param Description
handlerName The name of the trigger handler class to check for

Returns

Type Description
Boolean Boolean

Example

TriggerHandler.isBypassed('AccountTriggerHandler');

public static void clearAllBypasses()

Inherited

removes all classes from the bypass list

Example

Triggerhandler.clearAllBypasses();

protected void addToLoopCount()

Inherited

TESTVISIBLE

increment the loop count

Throws

Exception Description
Throws loop count exception if the max loop count is reached

protected Boolean validateRun()

Inherited

TESTVISIBLE

make sure this trigger should continue to run

Returns

Type Description
Boolean Boolean

Throws

Exception Description
TriggerHandlerException thrown when executing outside of a trigger

Classes

AccountTriggerHandlerException

Custom exception class

Inheritance

AccountTriggerHandlerException


Clone this wiki locally