Skip to content

AccountTriggerHandler

pozil edited this page Nov 14, 2024 · 17 revisions

AccountTriggerHandler Class

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

Group Trigger Recipes

See TriggerHandler

See AccountServiceLayer

Inheritance

TriggerHandler

Fields

triggerNew

Signature

private triggerNew

Type

List<Account>


triggerOld

Signature

private triggerOld

Type

List<Account>


triggerMapNew

Signature

private triggerMapNew

Type

Map<Id,Account>


triggerMapOld

Signature

private triggerMapOld

Type

Map<Id,Account>


circuitBreaker

TESTVISIBLE

Signature

private static circuitBreaker

Type

Exception


context

Inherited

TESTVISIBLE

Signature

protected context

Type

TriggerContext

Constructors

AccountTriggerHandler()

Constructor that sets class variables based on Trigger context vars

Signature

public AccountTriggerHandler()

Methods

beforeInsert()

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

Signature

public override void beforeInsert()

Return Type

void

Example

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

afterInsert()

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

Signature

public override void afterInsert()

Return Type

void

Example

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

beforeUpdate()

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

Signature

public override void beforeUpdate()

Return Type

void

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;

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.

Signature

public override void afterUpdate()

Return Type

void

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;

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.

Signature

public override void beforeDelete()

Return Type

void

Example

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

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.

Signature

public override void afterDelete()

Return Type

void

Example

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

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.

Signature

public override void afterUndelete()

Return Type

void

Example

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

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

Signature

public virtual void run()

Return Type

void

Example

AccountTriggerHandler.run();

setMaxLoopCount(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.

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()

Inherited

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)

Inherited

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)

Inherited

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)

Inherited

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()

Inherited

removes all classes from the bypass list

Signature

public static void clearAllBypasses()

Return Type

void

Example

Triggerhandler.clearAllBypasses();

addToLoopCount()

Inherited

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()

Inherited

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

Classes

AccountTriggerHandlerException Class

Custom exception class

Clone this wiki locally