-
Notifications
You must be signed in to change notification settings - Fork 458
AccountTriggerHandler
Demonstrates how to construct a TriggerHandler using the trigger handler framework found in Shared Code/TriggerHandler.cls
Group Trigger Recipes
See TriggerHandler
Inheritance
private triggerNew
List<Account>
private triggerOld
List<Account>
private triggerMapNew
Map<Id,Account>
private triggerMapOld
Map<Id,Account>
TESTVISIBLE
private static circuitBreaker
Exception
Inherited
TESTVISIBLE
protected context
TriggerContext
Constructor that sets class variables based on Trigger context vars
public AccountTriggerHandler()
Before Insert context method. Called automatically by the trigger framework this class extends.
public override void beforeInsert()
void
Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;
after insert context method. Called automatically by the trigger framework this class extends
public override void afterInsert()
void
Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
accounts.add(new Account(name='example 2'));
insert accounts;
before update context method. Called automatically by the trigger framework this class extends
public override void beforeUpdate()
void
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;
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.
public override void afterUpdate()
void
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;
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.
public override void beforeDelete()
void
Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;
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.
public override void afterDelete()
void
Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;
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.
public override void afterUndelete()
void
Account[] accounts = new Account[]();
accounts.add(new Account(name='example 1'));
insert accounts;
delete accounts;
undelete accounts;
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
public virtual void run()
void
AccountTriggerHandler.run();
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
Custom exception class