-
afterDelete ()
Runs in the after delete context. Override it in the handler. The base method does nothing.
-
afterInsert ()
Runs in the after insert context. Override it in the handler. The base method does nothing.
-
afterUndelete ()
Runs in the after undelete context. Override it in the handler. The base method does nothing.
-
afterUpdate ()
Runs in the after update context. Override it in the handler. The base method does nothing.
-
andFinally ()
Runs after the context method returns, in all trigger contexts. It also runs in a context that the handler does not override.
It does not run when the handler is bypassed, or when the running user has the
BypassTriggerHandlers permission. Then nothing in the handler runs.
It does not run when
the loop limit stops the run. Then only log() runs, with the loop limit warning.
It does
not run when the context method throws an exception that it does not catch.
It runs when the context method catches its
own exception or calls addError().
After a run starts its context method, a bypass cannot
stop the andFinally() of that run. For example, the context method bypasses this handler and
updates records of the same object. The handler skips the run for that update, and the
andFinally() of the first run comes last.
If
andFinally() throws, the exception reaches the caller, and the handler does not write the exit
and limits lines.
Credit to James Simone for this idea.
-
beforeDelete ()
Runs in the before delete context. Override it in the handler. The base method does nothing.
-
beforeInsert ()
Runs in the before insert context. Override it in the handler. The base method does nothing.
-
beforeUpdate ()
Runs in the before update context. Override it in the handler. The base method does nothing.
-
bypass (handlerName)
Bypasses a handler by name. The name matches in any case.
-
bypass (handlerType)
Bypasses a handler by type. The compiler checks the type, so the name cannot have a typo.
-
bypass (handlerNames)
Bypasses a list of handlers by name. It checks all names before it bypasses a handler.
-
bypass (handlerNames)
Bypasses a set of handlers by name. It calls bypass(List<String>).
-
bypassAll ()
Bypasses all handlers. The named bypasses stay, and they apply again after clearGlobalBypass().
-
bypassList ()
Returns the handlers that are bypassed by name or by type, in sorted order, as the caller wrote them.
The list does not
include the global bypass. For the global bypass, use isGlobalBypassed().
It replaces the
deprecated bypassSet(). A List also has
contains(value).
-
bypassSet ()
Returns the handlers that are bypassed by name or by type, as the caller wrote them.
The set does not include the global
bypass. For the global bypass, use isGlobalBypassed().
-
clearAllBypasses ()
Clears all bypasses: the global bypass and all named bypasses.
-
clearBypass (handlerName)
Clears the bypass of a handler by name. The name matches in any case.
-
clearBypass (handlerType)
Clears the bypass of a handler by type.
-
clearBypass (handlerNames)
Clears the bypass of a list of handlers by name. It checks all names before it clears a bypass.
-
clearBypass (handlerNames)
Clears the bypass of a set of handlers by name. It calls clearBypass(List<String>).
-
clearBypassList ()
Clears all named bypasses. The global bypass stays.
Use it to reset the named bypasses while all handlers stay bypassed.
-
clearGlobalBypass ()
Clears only the global bypass. The named bypasses stay.
Use it to keep a set of bypassed handlers for a full operation.
After bypassAll() and clearGlobalBypass(), the handlers that
were bypassed by name are still bypassed.
-
clearMaxLoopCount ()
Removes the limit of all trigger operations of this handler. The handler keeps its counters, so it still counts its runs.
-
debug ()
Makes all handlers that run later in the transaction write the entry and exit lines. You can chain this method.
The
setting is static, so it applies to all handlers, not only to this handler.
-
debug (enabled)
Sets whether all handlers that run later in the transaction write the entry and exit lines. You can chain this method.
The
setting is static, so it applies to all handlers, not only to this handler.
-
getHandlerName ()
Returns the name of this handler: Type.getName() from the Type constructor, or the runtime
class name.
A subclass can use it, for example in a log line or in a bypass() call.
The runtime lookup reads the start of toString(). For an inner class, it gives only the
short name. A handler that is an inner class, or that overrides toString(), must pass its type
to the Type constructor.
-
getLoopCount (handlerName)
Returns the number of runs of a handler in this transaction, for all trigger operations together. The count includes the runs
that the loop limit stopped.
The name must be the handler name in the same case, as
getHandlerName() returns it. Unlike a bypass name, it does not match in any case.
-
getLoopCount (handlerName, operation)
Returns the number of runs of 1 trigger operation of a handler in this transaction. The count includes the runs that the loop
limit stopped.
The name must be the handler name in the same case, as
getHandlerName() returns it. Unlike a bypass name, it does not match in any case.
-
getLoopCount (handlerType, operation)
Returns the number of runs of 1 trigger operation of a handler in this transaction, by type. The compiler checks the type, so
the name has the correct case.
-
getTriggerEvent ()
Returns the trigger operation of the current run. In a test that runs the handler without DML,
setTestContext() sets it, because Trigger.operationType is
null there.
-
hasBypassPermission ()
Tells if the running user has the BypassTriggerHandlers custom permission. Such a user runs no
handler, as if all handlers were bypassed.
Code cannot clear this bypass:
clearAllBypasses() and clearGlobalBypass() do not change it.
An admin assigns the permission through a permission set, for work such as a large data load.
-
hideAllDebug ()
Stops the entry and exit lines of all handlers that run later in the transaction. It wins over
showDebug() and over the setting of each handler, so no other call can turn the lines back on.
To write the lines again, call showAllDebug() or
hideAllDebug(false). The handlers then use their earlier settings. It does not stop the limits
line or the loop limit warning.
-
hideAllDebug (hidden)
Sets whether the entry and exit lines of all handlers that run later in the transaction are hidden. While they are hidden,
showDebug() and showHandlerDebug() have no effect.
-
incrementCheckLoopCount ()
Adds this run to the counter of the current trigger operation, when the operation has a counter. Then it checks the limit.
When the count passes the limit, it writes a warning. run() then returns before the
context method and andFinally().
-
isBypassed (handlerName)
Tells if a handler is bypassed by name or by type. The result does not include the global bypass. For the global bypass, use
isGlobalBypassed(). For both, use isSkipped().
-
isBypassed (handlerType)
Tells if a handler is bypassed by name or by type. The result does not include the global bypass. For the global bypass, use
isGlobalBypassed(). For both, use isSkipped().
-
isDebugOn ()
Tells if this handler writes the entry and exit lines. It uses the setting of the handler, or the static setting when the
handler has none. hideAllDebug() wins over both.
-
isGlobalBypassed ()
Tells if all handlers are bypassed, after bypassAll().
-
isLimitsOn ()
Tells if this handler writes the limits line. It uses the setting of the handler, or the static setting when the handler has
none.
-
isSkipped (handlerName)
Tells if a handler does not run. A handler does not run when it is bypassed by name or by type, when all handlers are bypassed,
or when the running user has the BypassTriggerHandlers permission. The result does not include
the loop limit.
-
isSkipped (handlerType)
Tells if a handler does not run. A handler does not run when it is bypassed by name or by type, when all handlers are bypassed,
or when the running user has the BypassTriggerHandlers permission. The result does not include
the loop limit.
-
limits ()
Makes all handlers that run later in the transaction write the limits line. You can chain this method.
The setting is
static, so it applies to all handlers, not only to this handler.
-
limits (enabled)
Sets whether all handlers that run later in the transaction write the limits line. You can chain this method.
The setting
is static, so it applies to all handlers, not only to this handler.
-
log (level, message)
Writes a message from the framework. The messages are the entry, exit, and limits lines when they are on, and the warning when a
run passes the loop limit.
The default writes to the debug log. Override it to send the messages to the logging framework
of your org, such as
Nebula Logger. The README has an
example.
-
requireHandlerName (handlerName)
Rejects a null or blank handler name.
-
requireHandlerNames (handlerNames)
Rejects a null list, or a list with a null or blank name, before the method uses a name.
-
requireHandlerType (handlerType)
Rejects a null handler type.
-
run ()
Runs the handler. Call it from the trigger. It calls the context method of the current trigger operation, and then
andFinally().
It does nothing when the handler is bypassed, or when the running user has
the BypassTriggerHandlers permission. When the loop limit stops the run, it only sends a
warning to log().
The switch names all 7 trigger operations, and
validateRun() rejects a null operation. So this method suppresses
SwitchWithoutWhenElse.
-
setBypass (handlerName, desiredValue)
Sets the named bypass of a handler to a value. You do not have to know the current status first.
false
clears only the named bypass. A global bypass stays, so the handler does not run until
clearGlobalBypass().
isBypassed() tells only about the
named bypass. So the save-and-restore example gives back the exact earlier state.
-
setBypass (handlerType, desiredValue)
Sets the named bypass of a handler, given by type, to a value. It works as the String form does, with
Type.getName() as the name.
-
setMaxLoopCount (max)
Limits how many times run() runs this handler. Each trigger operation gets the same limit.
Each operation has its own count for the transaction. The count starts at the first call of
setMaxLoopCount(), also with 0 or a negative value. Each chunk of 200 records counts as 1 run.
A bypassed run does not count. A run that the limit stops counts.
When an operation passes its limit,
run() sends a warning to log() and returns. It does not call
the context method or andFinally().
In a partial-success DML statement, Salesforce rolls
back and runs the trigger again for the records that did not fail. Static variables keep their values, so each retry counts as
another run. A low limit can stop the retry, and then the handler work for the saved records does not happen. The README tells
how to set the limit for this case.
-
setMaxLoopCount (operation, max)
Limits how many times run() runs this handler for 1 trigger operation. The other operations
keep their limits.
The count of the operation starts at the first call of
setMaxLoopCount() for it, also with 0 or a negative value. Each chunk of 200 records counts as
1 run. A bypassed run does not count. A run that the limit stops counts.
Each retry of a partial-success DML statement
counts as another run, as setMaxLoopCount(Integer) tells.
-
setTestContext (operation)
Makes this handler run as if a trigger fired the operation. A test can then call run() without
DML.
Before you call run(), set the records that the handler reads, for example through
@TestVisible fields of the handler.
Outside a test, it changes nothing. The handler keeps
the context of the real trigger.
A null operation clears the context. run() then throws,
as it does outside a trigger.
-
setTriggerContext ()
Sets the current context from Trigger.operationType. Both constructors call it.
Outside a
trigger, isTriggerExecuting is false. A test sets its own context with
setTestContext().
-
showAllDebug ()
Ends hideAllDebug(). Each handler then uses its own setting or the static setting again. It is
the same as hideAllDebug(false).
It does not turn the lines on. A handler writes them
only when its setting or showDebug() turns them on.
-
showDebug ()
Makes all handlers that run later in the transaction write the entry and exit lines. A handler with its own setting from
showHandlerDebug() keeps it. hideAllDebug() wins over this
setting.
Call it in the trigger or in a handler.
-
showDebug (enabled)
Sets whether all handlers that run later in the transaction write the entry and exit lines. A handler with its own setting from
showHandlerDebug() keeps it. hideAllDebug() wins over this
setting.
-
showHandlerDebug ()
Makes this handler write the entry and exit lines. Other handlers keep their own setting or the static setting.
hideAllDebug() wins over this setting.
-
showHandlerDebug (enabled)
Sets whether this handler writes the entry and exit lines. This setting wins over the static
showDebug(). hideAllDebug() wins over this setting.
-
showHandlerLimits ()
Makes this handler write the limits line. Other handlers keep their own setting or the static setting.
-
showHandlerLimits (enabled)
Sets whether this handler writes the limits line. This setting wins over the static
showLimits().
-
showLimits ()
Makes all handlers that run later in the transaction write the limits line. A handler with its own setting from
showHandlerLimits() keeps it.
Call it in the trigger or in a handler.
-
showLimits (enabled)
Sets whether all handlers that run later in the transaction write the limits line. A handler with its own setting from
showHandlerLimits() keeps it.
-
validateRun ()
Tells if this run continues. With false, run() returns before
the context method.
Runs in the after delete context. Override it in the handler. The base method does nothing.
Signature
protected virtual void afterDelete()
Since
1.0.0
Runs in the after insert context. Override it in the handler. The base method does nothing.
Signature
protected virtual void afterInsert()
Since
1.0.0
Runs in the after undelete context. Override it in the handler. The base method does nothing.
Signature
protected virtual void afterUndelete()
Since
1.0.0
Runs in the after update context. Override it in the handler. The base method does nothing.
Signature
protected virtual void afterUpdate()
Since
1.0.0
Runs after the context method returns, in all trigger contexts. It also runs in a context that the handler does not override.
It does not run when the handler is bypassed, or when the running user has the
BypassTriggerHandlers permission. Then nothing in the handler runs.
It does not run when
the loop limit stops the run. Then only log() runs, with the loop limit warning.
It does
not run when the context method throws an exception that it does not catch.
It runs when the context method catches its own
exception or calls addError().
After a run starts its context method, a bypass cannot stop
the andFinally() of that run. For example, the context method bypasses this handler and updates
records of the same object. The handler skips the run for that update, and the andFinally() of
the first run comes last.
If andFinally() throws, the exception reaches the caller, and the
handler does not write the exit and limits lines.
Credit to James Simone for this idea.
Signature
protected virtual void andFinally()
See
Author
James Simone
Since
1.1.0
Example
// andFinally() runs after the before context and again after the after context.
// This example runs only once for each chunk of 200 records, after the after context.
public override void andFinally() {
if (getTriggerEvent() == System.TriggerOperation.AFTER_UPDATE) {
EventBus.publish(new Account_Change__e(Record_Count__c = Trigger.size));
}
}
Runs in the before delete context. Override it in the handler. The base method does nothing.
Signature
protected virtual void beforeDelete()
Since
1.0.0
Runs in the before insert context. Override it in the handler. The base method does nothing.
Signature
protected virtual void beforeInsert()
Since
1.0.0
Runs in the before update context. Override it in the handler. The base method does nothing.
Signature
protected virtual void beforeUpdate()
Since
1.0.0
Bypasses a handler by name. The name matches in any case.
Signature
public static void bypass(String handlerName)
Parameters
handlerName
Type: String
The name of the handler to bypass.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
Since
1.0.0
Example
TriggerHandler.bypass('AccountSampleTriggerHandler');
Bypasses a handler by type. The compiler checks the type, so the name cannot have a typo.
Signature
public static void bypass(Type handlerType)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
Exceptions
TriggerHandlerException when the type is null. @throws TriggerHandlerException when the type is null.
Author
Since
1.0.0
Example
TriggerHandler.bypass(AccountSampleTriggerHandler.class);
Bypasses a list of handlers by name. It checks all names before it bypasses a handler.
Signature
public static void bypass(List<String> handlerNames)
Parameters
handlerNames
Type: List<String>
The names of the handlers to bypass.
Exceptions
TriggerHandlerException when the list is null, or a name in it is null or blank. @throws TriggerHandlerException when the list is
null, or a name in it is null or blank.
Since
1.0.0
Example
TriggerHandler.bypass(new List<String>{ 'AccountSampleTriggerHandler', 'ContactTriggerHandler' });
Bypasses a set of handlers by name. It calls bypass(List<String>).
Signature
public static void bypass(Set<String> handlerNames)
Parameters
handlerNames
Type: Set<String>
The names of the handlers to bypass.
Exceptions
TriggerHandlerException when the set is null, or a name in it is null or blank. @throws TriggerHandlerException when the set is
null, or a name in it is null or blank.
See
TriggerHandler.bypass
Since
2.1.0
Example
TriggerHandler.bypass(new Set<String>{ 'AccountSampleTriggerHandler', 'ContactTriggerHandler' });
Bypasses all handlers. The named bypasses stay, and they apply again after clearGlobalBypass().
Signature
public static void bypassAll()
Since
1.0.0
Example
TriggerHandler.bypassAll();
Returns the handlers that are bypassed by name or by type, in sorted order, as the caller wrote them.
The list does not
include the global bypass. For the global bypass, use isGlobalBypassed().
It replaces the
deprecated bypassSet(). A List also has
contains(value).
Signature
public static List<String> bypassList()
Returns
List<String> The bypassed handlers.
Since
1.0.0
Example
if (TriggerHandler.bypassList().contains('AccountSampleTriggerHandler')) {
// the handler is bypassed by name
}
Returns the handlers that are bypassed by name or by type, as the caller wrote them.
The set does not include the global
bypass. For the global bypass, use isGlobalBypassed().
Signature
public static Set<String> bypassSet()
Deprecated
Use bypassList(), which holds the same names in sorted order. A later major release removes this
method.
Returns
Set<String> The bypassed handlers.
See
TriggerHandler.bypassList
Author
David Schach
Since
1.0.0
Example
// before
Boolean bypassed = TriggerHandler.bypassSet().contains('AccountSampleTriggerHandler');
// after
bypassed = TriggerHandler.bypassList().contains('AccountSampleTriggerHandler');
Clears all bypasses: the global bypass and all named bypasses.
Signature
public static void clearAllBypasses()
Since
1.0.0
Example
TriggerHandler.clearAllBypasses();
Clears the bypass of a handler by name. The name matches in any case.
Signature
public static void clearBypass(String handlerName)
Parameters
handlerName
Type: String
The name of the handler.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
Author
Since
1.0.0
Example
TriggerHandler.clearBypass('AccountSampleTriggerHandler');
Clears the bypass of a handler by type.
Signature
public static void clearBypass(Type handlerType)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
Exceptions
TriggerHandlerException when the type is null. @throws TriggerHandlerException when the type is null.
Since
1.0.0
Example
TriggerHandler.clearBypass(AccountSampleTriggerHandler.class);
Clears the bypass of a list of handlers by name. It checks all names before it clears a bypass.
Signature
public static void clearBypass(List<String> handlerNames)
Parameters
handlerNames
Type: List<String>
The names of the handlers.
Exceptions
TriggerHandlerException when the list is null, or a name in it is null or blank. @throws TriggerHandlerException when the list is
null, or a name in it is null or blank.
Since
1.0.0
Example
TriggerHandler.clearBypass(new List<String>{ 'AccountSampleTriggerHandler', 'ContactTriggerHandler' });
Clears the bypass of a set of handlers by name. It calls clearBypass(List<String>).
Signature
public static void clearBypass(Set<String> handlerNames)
Parameters
handlerNames
Type: Set<String>
The names of the handlers.
Exceptions
TriggerHandlerException when the set is null, or a name in it is null or blank. @throws TriggerHandlerException when the set is
null, or a name in it is null or blank.
See
TriggerHandler.clearBypass
Since
2.1.0
Example
TriggerHandler.clearBypass(new Set<String>{ 'AccountSampleTriggerHandler', 'ContactTriggerHandler' });
Clears all named bypasses. The global bypass stays.
Use it to reset the named bypasses while all handlers stay bypassed.
Signature
public static void clearBypassList()
Since
1.0.0
Example
TriggerHandler.clearBypassList();
Clears only the global bypass. The named bypasses stay.
Use it to keep a set of bypassed handlers for a full operation.
After bypassAll() and clearGlobalBypass(), the handlers that
were bypassed by name are still bypassed.
Signature
public static void clearGlobalBypass()
Since
1.0.0
Example
TriggerHandler.clearGlobalBypass();
Removes the limit of all trigger operations of this handler. The handler keeps its counters, so it still counts its runs.
Signature
public TriggerHandler clearMaxLoopCount()
Returns
TriggerHandler This handler, so that you can chain calls.
Since
1.0.0
2.1.0 Return the handler, so that you can chain calls
Example
clearMaxLoopCount(); // in the handler
// or in the trigger
new AccountSampleTriggerHandler().clearMaxLoopCount().run();
Makes all handlers that run later in the transaction write the entry and exit lines. You can chain this method.
The setting
is static, so it applies to all handlers, not only to this handler.
Signature
public TriggerHandler debug()
Deprecated
Use showHandlerDebug() for this handler only, or the static
showDebug() for all handlers. A later major release removes this method.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showDebug,
TriggerHandler.showHandlerDebug
Since
1.2.0
Example
// before
new AccountSampleTriggerHandler().debug().run();
// after, for this handler only
new AccountSampleTriggerHandler().showHandlerDebug().run();
Sets whether all handlers that run later in the transaction write the entry and exit lines. You can chain this method.
The
setting is static, so it applies to all handlers, not only to this handler.
Signature
public TriggerHandler debug(Boolean enabled)
Deprecated
Use showHandlerDebug(Boolean) for this handler only, or the static
showDebug(Boolean) for all handlers. A later major release removes this method.
Parameters
enabled
Type: Boolean
true to write the lines, false or null to stop them.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showDebug,
TriggerHandler.showHandlerDebug
Since
1.2.0
Example
// before
new AccountSampleTriggerHandler().debug(false).run();
// after, for this handler only
new AccountSampleTriggerHandler().showHandlerDebug(false).run();
Returns the name of this handler: Type.getName() from the Type constructor, or the runtime class
name.
A subclass can use it, for example in a log line or in a bypass() call.
The
runtime lookup reads the start of toString(). For an inner class, it gives only the short name. A
handler that is an inner class, or that overrides toString(), must pass its type to the Type
constructor.
Signature
@TestVisible
protected String getHandlerName()
Returns
String The name of this handler.
Since
1.4.0
Returns the number of runs of a handler in this transaction, for all trigger operations together. The count includes the runs that
the loop limit stopped.
The name must be the handler name in the same case, as
getHandlerName() returns it. Unlike a bypass name, it does not match in any case.
Signature
public static Integer getLoopCount(String handlerName)
Deprecated
Use getLoopCount(String, System.TriggerOperation) for the count of 1 trigger operation. A later
major release removes this method.
Parameters
handlerName
Type: String
The name of the handler.
Returns
Integer The total count, or -1 when setMaxLoopCount() was never
called for the handler.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
See
TriggerHandler.getLoopCount
Since
1.0.0
Example
Integer runs = TriggerHandler.getLoopCount('AccountSampleTriggerHandler');
Returns the number of runs of 1 trigger operation of a handler in this transaction. The count includes the runs that the loop
limit stopped.
The name must be the handler name in the same case, as
getHandlerName() returns it. Unlike a bypass name, it does not match in any case.
Signature
public static Integer getLoopCount(String handlerName, System.TriggerOperation operation)
Parameters
handlerName
Type: String
The name of the handler.
operation
Type: System.TriggerOperation
The trigger operation.
Returns
Integer The count, or -1 when setMaxLoopCount() was never called
for the operation.
Exceptions
TriggerHandlerException when the name is null or blank, or the operation is null. @throws TriggerHandlerException when the name is
null or blank, or the operation is null.
Since
2.0.0
Example
Integer runs = TriggerHandler.getLoopCount('AccountSampleTriggerHandler', System.TriggerOperation.AFTER_UPDATE);
Returns the number of runs of 1 trigger operation of a handler in this transaction, by type. The compiler checks the type, so the
name has the correct case.
Signature
public static Integer getLoopCount(Type handlerType, System.TriggerOperation operation)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
operation
Type: System.TriggerOperation
The trigger operation.
Returns
Integer The count, or -1 when setMaxLoopCount() was never called
for the operation.
Exceptions
TriggerHandlerException when the type or the operation is null. @throws TriggerHandlerException when the type or the operation is
null.
See
TriggerHandler.getLoopCount
Since
2.1.0
Example
Integer runs = TriggerHandler.getLoopCount(AccountSampleTriggerHandler.class, System.TriggerOperation.AFTER_UPDATE);
Returns the trigger operation of the current run. In a test that runs the handler without DML,
setTestContext() sets it, because Trigger.operationType is null
there.
Signature
protected System.TriggerOperation getTriggerEvent()
Returns
System.TriggerOperation The current operation, or null outside a trigger.
Since
1.4.0
Tells if the running user has the BypassTriggerHandlers custom permission. Such a user runs no
handler, as if all handlers were bypassed.
Code cannot clear this bypass:
clearAllBypasses() and clearGlobalBypass() do not change it. An
admin assigns the permission through a permission set, for work such as a large data load.
Signature
public static Boolean hasBypassPermission()
Returns
Boolean true when the running user has the permission.
See
TriggerHandler.isSkipped
Since
2.1.0
Example
if (TriggerHandler.hasBypassPermission()) {
// no handler runs for this user
}
Stops the entry and exit lines of all handlers that run later in the transaction. It wins over
showDebug() and over the setting of each handler, so no other call can turn the lines back on.
To write the lines again, call showAllDebug() or
hideAllDebug(false). The handlers then use their earlier settings. It does not stop the limits
line or the loop limit warning.
Signature
public static void hideAllDebug()
See
TriggerHandler.hideAllDebug,
TriggerHandler.showAllDebug
Since
2.1.0
Example
TriggerHandler.hideAllDebug();
new AccountSampleTriggerHandler().showHandlerDebug().run(); // writes no entry or exit line
Sets whether the entry and exit lines of all handlers that run later in the transaction are hidden. While they are hidden,
showDebug() and showHandlerDebug() have no effect.
Signature
public static void hideAllDebug(Boolean hidden)
Parameters
hidden
Type: Boolean
true to hide the lines, false or null to use the debug settings
again.
See
TriggerHandler.hideAllDebug,
TriggerHandler.showAllDebug
Since
2.1.0
Example
TriggerHandler.hideAllDebug(false);
Adds this run to the counter of the current trigger operation, when the operation has a counter. Then it checks the limit.
When the count passes the limit, it writes a warning. run() then returns before the context
method and andFinally().
Signature
private Boolean incrementCheckLoopCount()
Returns
Boolean true when
run() continues: the operation has no counter, or the count is in the limit.
false when the count passes the limit.
Authors
Since
1.0.0
Tells if a handler is bypassed by name or by type. The result does not include the global bypass. For the global bypass, use
isGlobalBypassed(). For both, use isSkipped().
Signature
public static Boolean isBypassed(String handlerName)
Parameters
handlerName
Type: String
The name of the handler. It matches in any case.
Returns
Boolean true when the handler is bypassed by name or by type.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
See
TriggerHandler.isSkipped
Since
1.0.0
Example
if (TriggerHandler.isBypassed('AccountSampleTriggerHandler')) {
// the handler is bypassed by name or by type
}
Tells if a handler is bypassed by name or by type. The result does not include the global bypass. For the global bypass, use
isGlobalBypassed(). For both, use isSkipped().
Signature
public static Boolean isBypassed(Type handlerType)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
Returns
Boolean true when the handler is bypassed by name or by type.
Exceptions
TriggerHandlerException when the type is null. @throws TriggerHandlerException when the type is null.
See
TriggerHandler.isSkipped
Since
1.0.0
Example
if (TriggerHandler.isBypassed(AccountSampleTriggerHandler.class)) {
// the handler is bypassed by name or by type
}
Tells if this handler writes the entry and exit lines. It uses the setting of the handler, or the static setting when the handler
has none. hideAllDebug() wins over both.
Signature
private Boolean isDebugOn()
Returns
Boolean true when this handler writes the entry and exit lines.
Since
2.0.0
Tells if all handlers are bypassed, after bypassAll().
Signature
public static Boolean isGlobalBypassed()
Returns
Boolean true when the global bypass is on.
Since
2.0.0
Example
if (TriggerHandler.isGlobalBypassed()) {
// no handler runs
}
Tells if this handler writes the limits line. It uses the setting of the handler, or the static setting when the handler has none.
Signature
private Boolean isLimitsOn()
Returns
Boolean true when this handler writes the limits line.
Since
2.0.0
Tells if a handler does not run. A handler does not run when it is bypassed by name or by type, when all handlers are bypassed, or
when the running user has the BypassTriggerHandlers permission. The result does not include the
loop limit.
Signature
public static Boolean isSkipped(String handlerName)
Parameters
handlerName
Type: String
The name of the handler. It matches in any case.
Returns
Boolean true when the handler does not run.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
Since
2.0.0
Example
if (TriggerHandler.isSkipped('AccountSampleTriggerHandler')) {
// the handler does not run
}
Tells if a handler does not run. A handler does not run when it is bypassed by name or by type, when all handlers are bypassed, or
when the running user has the BypassTriggerHandlers permission. The result does not include the
loop limit.
Signature
public static Boolean isSkipped(Type handlerType)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
Returns
Boolean true when the handler does not run.
Exceptions
TriggerHandlerException when the type is null. @throws TriggerHandlerException when the type is null.
Since
2.0.0
Example
if (TriggerHandler.isSkipped(AccountSampleTriggerHandler.class)) {
// the handler does not run
}
Makes all handlers that run later in the transaction write the limits line. You can chain this method.
The setting is
static, so it applies to all handlers, not only to this handler.
Signature
public TriggerHandler limits()
Deprecated
Use showHandlerLimits() for this handler only, or the static
showLimits() for all handlers. A later major release removes this method.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showLimits,
TriggerHandler.showHandlerLimits
Since
1.2.0
Example
// before
new AccountSampleTriggerHandler().limits().run();
// after, for this handler only
new AccountSampleTriggerHandler().showHandlerLimits().run();
Sets whether all handlers that run later in the transaction write the limits line. You can chain this method.
The setting is
static, so it applies to all handlers, not only to this handler.
Signature
public TriggerHandler limits(Boolean enabled)
Deprecated
Use showHandlerLimits(Boolean) for this handler only, or the static
showLimits(Boolean) for all handlers. A later major release removes this method.
Parameters
enabled
Type: Boolean
true to write the line, false or null to stop it.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showLimits,
TriggerHandler.showHandlerLimits
Since
1.2.0
Example
// before
new AccountSampleTriggerHandler().limits(false).run();
// after, for this handler only
new AccountSampleTriggerHandler().showHandlerLimits(false).run();
Writes a message from the framework. The messages are the entry, exit, and limits lines when they are on, and the warning when a
run passes the loop limit.
The default writes to the debug log. Override it to send the messages to the logging framework of
your org, such as
Nebula Logger.
The README has an example.
Signature
protected virtual void log(LoggingLevel level, String message)
Parameters
level
Type: LoggingLevel
The logging level of the message.
message
Type: String
The message.
Since
1.5.0
Example
// in the handler class
protected override void log(LoggingLevel level, String message) {
// send the message to the logging framework here
super.log(level, message);
}
Rejects a null or blank handler name.
Signature
private static void requireHandlerName(String handlerName)
Parameters
handlerName
Type: String
The name to check.
Exceptions
TriggerHandlerException when the name is null or blank. @throws TriggerHandlerException when the name is null or blank.
Since
1.3.0
Rejects a null list, or a list with a null or blank name, before the method uses a name.
Signature
private static void requireHandlerNames(List<String> handlerNames)
Parameters
handlerNames
Type: List<String>
The names to check.
Exceptions
TriggerHandlerException when the list is null or has a null or blank name. @throws TriggerHandlerException when the list is null
or has a null or blank name.
Since
1.3.0
Rejects a null handler type.
Signature
private static void requireHandlerType(Type handlerType)
Parameters
handlerType
Type: Type
The type to check.
Exceptions
TriggerHandlerException when the type is null. @throws TriggerHandlerException when the type is null.
Since
1.3.0
Runs the handler. Call it from the trigger. It calls the context method of the current trigger operation, and then
andFinally().
It does nothing when the handler is bypassed, or when the running user has
the BypassTriggerHandlers permission. When the loop limit stops the run, it only sends a warning
to log().
The switch names all 7 trigger operations, and
validateRun() rejects a null operation. So this method suppresses
SwitchWithoutWhenElse.
Signature
@SuppressWarnings('PMD.SwitchWithoutWhenElse')
public void run()
Exceptions
TriggerHandlerException when run() is called outside a trigger. @throws TriggerHandlerException
when run() is called outside a trigger.
Authors
Since
1.0.0
Example
trigger AccountSampleTrigger on Account(before insert, before update, before delete, after insert, after update, after delete, after undelete) {
new AccountSampleTriggerHandler().run();
}
Sets the named bypass of a handler to a value. You do not have to know the current status first.
false
clears only the named bypass. A global bypass stays, so the handler does not run until
clearGlobalBypass().
isBypassed() tells only about the
named bypass. So the save-and-restore example gives back the exact earlier state.
Signature
public static void setBypass(String handlerName, Boolean desiredValue)
Parameters
handlerName
Type: String
The name of the handler.
desiredValue
Type: Boolean
true to bypass the handler, false to clear its named bypass.
Exceptions
TriggerHandlerException when the name is null or blank, or the value is null. @throws TriggerHandlerException when the name is
null or blank, or the value is null.
See
TriggerHandler.setBypass
Since
1.0.0
Author
Example
TriggerHandler.setBypass('AccountSampleTriggerHandler', false);
// or save the state, bypass the handler, and restore the state
Boolean wasBypassed = TriggerHandler.isBypassed('AccountSampleTriggerHandler');
TriggerHandler.bypass('AccountSampleTriggerHandler');
// update the records here
TriggerHandler.setBypass('AccountSampleTriggerHandler', wasBypassed);
Sets the named bypass of a handler, given by type, to a value. It works as the String form does, with
Type.getName() as the name.
Signature
public static void setBypass(Type handlerType, Boolean desiredValue)
Parameters
handlerType
Type: Type
The handler class, written as ClassName.class.
desiredValue
Type: Boolean
true to bypass the handler, false to clear its named bypass.
Exceptions
TriggerHandlerException when the type is null, or the value is null. @throws TriggerHandlerException when the type is null, or the
value is null.
See
TriggerHandler.setBypass
Since
2.0.0
Example
Boolean wasBypassed = TriggerHandler.isBypassed(AccountSampleTriggerHandler.class);
TriggerHandler.bypass(AccountSampleTriggerHandler.class);
// update the records here
TriggerHandler.setBypass(AccountSampleTriggerHandler.class, wasBypassed);
Limits how many times run() runs this handler. Each trigger operation gets the same limit.
Each operation has its own count for the transaction. The count starts at the first call of
setMaxLoopCount(), also with 0 or a negative value. Each chunk of 200 records counts as 1 run. A
bypassed run does not count. A run that the limit stops counts.
When an operation passes its limit,
run() sends a warning to log() and returns. It does not call the
context method or andFinally().
In a partial-success DML statement, Salesforce rolls back
and runs the trigger again for the records that did not fail. Static variables keep their values, so each retry counts as another
run. A low limit can stop the retry, and then the handler work for the saved records does not happen. The README tells how to set
the limit for this case.
Signature
public TriggerHandler setMaxLoopCount(Integer max)
Parameters
max
Type: Integer
The maximum number of runs of each operation. 0 or a negative value removes the limit.
Returns
TriggerHandler This handler, so that you can chain calls.
Exceptions
TriggerHandlerException when the value is null. @throws TriggerHandlerException when the value is null.
See
TriggerHandler.setMaxLoopCount
Since
1.0.0
Example
this.setMaxLoopCount(5); // in the handler constructor
// or in the trigger
new AccountSampleTriggerHandler().setMaxLoopCount(5).run();
Limits how many times run() runs this handler for 1 trigger operation. The other operations keep
their limits.
The count of the operation starts at the first call of setMaxLoopCount() for
it, also with 0 or a negative value. Each chunk of 200 records counts as 1 run. A bypassed run does not count. A run that the
limit stops counts.
Each retry of a partial-success DML statement counts as another run, as
setMaxLoopCount(Integer) tells.
Signature
public TriggerHandler setMaxLoopCount(System.TriggerOperation operation, Integer max)
Parameters
operation
Type: System.TriggerOperation
The trigger operation to limit.
max
Type: Integer
The maximum number of runs of the operation. 0 or a negative value removes the limit.
Returns
TriggerHandler This handler, so that you can chain calls.
Exceptions
TriggerHandlerException when the operation or the value is null. @throws TriggerHandlerException when the operation or the value
is null.
See
TriggerHandler.setMaxLoopCount
Since
2.0.0
Example
this.setMaxLoopCount(System.TriggerOperation.AFTER_UPDATE, 1); // in the handler constructor
Makes this handler run as if a trigger fired the operation. A test can then call run() without
DML.
Before you call run(), set the records that the handler reads, for example through
@TestVisible fields of the handler.
Outside a test, it changes nothing. The handler keeps
the context of the real trigger.
A null operation clears the context. run() then throws, as
it does outside a trigger.
Signature
@TestVisible
protected TriggerHandler setTestContext(System.TriggerOperation operation)
Parameters
operation
Type: System.TriggerOperation
The trigger operation to simulate.
Returns
TriggerHandler This handler, so that you can chain calls.
Since
1.5.0
Example
AccountSampleTriggerHandler handler = new AccountSampleTriggerHandler();
// set the records that the handler reads, and then:
handler.setTestContext(System.TriggerOperation.BEFORE_INSERT).run();
Sets the current context from Trigger.operationType. Both constructors call it.
Outside a
trigger, isTriggerExecuting is false. A test sets its own context with
setTestContext().
Signature
private void setTriggerContext()
Since
1.0.0
Ends hideAllDebug(). Each handler then uses its own setting or the static setting again. It is
the same as hideAllDebug(false).
It does not turn the lines on. A handler writes them only
when its setting or showDebug() turns them on.
Signature
public static void showAllDebug()
See
TriggerHandler.hideAllDebug,
TriggerHandler.showDebug
Since
2.1.0
Example
TriggerHandler.hideAllDebug();
// run the code that must write no entry or exit line
TriggerHandler.showAllDebug();
Makes all handlers that run later in the transaction write the entry and exit lines. A handler with its own setting from
showHandlerDebug() keeps it. hideAllDebug() wins over this
setting.
Call it in the trigger or in a handler.
Signature
public static void showDebug()
See
TriggerHandler.showDebug
Author
Since
1.0.0
Example
TriggerHandler.showDebug(); // in the trigger, before the handler runs
new AccountSampleTriggerHandler().run();
Sets whether all handlers that run later in the transaction write the entry and exit lines. A handler with its own setting from
showHandlerDebug() keeps it. hideAllDebug() wins over this
setting.
Signature
public static void showDebug(Boolean enabled)
Parameters
enabled
Type: Boolean
true to write the lines, false or null to stop them.
See
TriggerHandler.showDebug
Author
Since
1.0.0
Example
TriggerHandler.showDebug(false);
Makes this handler write the entry and exit lines. Other handlers keep their own setting or the static setting.
hideAllDebug() wins over this setting.
Signature
public TriggerHandler showHandlerDebug()
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showHandlerDebug
Since
2.0.0
Example
new AccountSampleTriggerHandler().showHandlerDebug().run();
Sets whether this handler writes the entry and exit lines. This setting wins over the static
showDebug(). hideAllDebug() wins over this setting.
Signature
public TriggerHandler showHandlerDebug(Boolean enabled)
Parameters
enabled
Type: Boolean
true to write the lines, false to stop them for this handler, or
null to use the static setting again.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showHandlerDebug
Since
2.0.0
Example
new AccountSampleTriggerHandler().showHandlerDebug(false).run();
Makes this handler write the limits line. Other handlers keep their own setting or the static setting.
Signature
public TriggerHandler showHandlerLimits()
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showHandlerLimits
Since
2.0.0
Example
new AccountSampleTriggerHandler().showHandlerLimits().run();
Sets whether this handler writes the limits line. This setting wins over the static showLimits().
Signature
public TriggerHandler showHandlerLimits(Boolean enabled)
Parameters
enabled
Type: Boolean
true to write the line, false to stop it for this handler, or
null to use the static setting again.
Returns
TriggerHandler This handler, so that you can chain calls.
See
TriggerHandler.showHandlerLimits
Since
2.0.0
Example
new AccountSampleTriggerHandler().showHandlerLimits(false).run();
Makes all handlers that run later in the transaction write the limits line. A handler with its own setting from
showHandlerLimits() keeps it.
Call it in the trigger or in a handler.
Signature
public static void showLimits()
See
TriggerHandler.showLimits
Since
1.0.0
Example
TriggerHandler.showLimits(); // in the trigger, before the handler runs
new AccountSampleTriggerHandler().run();
Sets whether all handlers that run later in the transaction write the limits line. A handler with its own setting from
showHandlerLimits() keeps it.
Signature
public static void showLimits(Boolean enabled)
Parameters
enabled
Type: Boolean
true to write the line, false or null to stop it.
See
TriggerHandler.showLimits
Since
1.0.0
Example
TriggerHandler.showLimits(false);
Tells if this run continues. With false, run() returns before
the context method.
Signature
private Boolean validateRun()
Returns
Boolean true when the handler runs,
false when it is skipped.
Exceptions
TriggerHandlerException when run() is called outside a trigger. @throws TriggerHandlerException
when run() is called outside a trigger.
See
TriggerHandler.TriggerHandlerException
Since
1.0.0