Show: | |||||||||||||||||||||||||||||||||||||||||||||||||||||
TriggerHandlerTrigger Handler virtual class as base for all trigger handlers
Signature
public virtual class TriggerHandler
See
Author
Since
Author
Since
TriggerHandler Properties
TriggerHandler Constructors
TriggerHandler()Basic constructor. Slower than the other one
Signature
public TriggerHandler()
See
Example
TriggerHandler(handlerName)Constructor with handler name to improve performance
Signature
public TriggerHandler(String handlerName)
Parameters
handlerName
Type:
String The name of the handler
Author
Since
Example
TriggerHandler Methods
afterDelete()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void afterDelete()
afterInsert()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void afterInsert()
afterUndelete()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void afterUndelete()
afterUpdate()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void afterUpdate()
andFinally()andFinally is called in every context, regardless of Trigger context Credit to James Simone for this idea Signature
@TestVisible
protected virtual void andFinally()
See
Author
James Simone
Since
beforeDelete()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void beforeDelete()
beforeInsert()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void beforeInsert()
beforeUpdate()Virtual method for the implementing class to override
Signature
@TestVisible
protected virtual void beforeUpdate()
bypass(handlerName)Bypass by string
Signature
public static void bypass(String handlerName)
Parameters
handlerName
Type:
String Name of the handler to be bypassed
Example
bypass(handlerType)Bypass by type/class. This is probably best for avoiding typos.
Signature
public static void bypass(Type handlerType)
Parameters
handlerType
Type:
Type The Class to be bypassed. Must end with ".class"
Author
Example
bypass(handlerNames)Bypass by list, e.g. TriggerHandler.bypass(listOfHandlerStrings)
Signature
public static void bypass(List<String> handlerNames)
Parameters
handlerNames
Type:
List<String> List of handlernames
bypassAll()Bypass all handlers (clear bypassedHandlers to prevent confusion)
Signature
public static void bypassAll()
Example
bypassList()
Return a list of the bypassed handlers
Though both Set and List allow contains(value), we include both methods for convenience Signature
public static List<String> bypassList()
Returns
List<String> List of bypassed handlersExample
bypassSet()
Return a Set of the bypassed handlers
Though both Set and List allow contains(value), we include both methods for convenience Signature
public static Set<String> bypassSet()
Returns
Set<String> Set of bypassed handlersAuthor
David Schach
Since
Example
clearAllBypasses()Clear all bypasses - by clearing the global bypass and by clearing the list of bypassed handlers
Signature
public static void clearAllBypasses()
Example
clearBypass(handlerName)Bypass a specific handler by name
Signature
public static void clearBypass(String handlerName)
Parameters
handlerName
Type:
String The class name to be bypassed
Author
Example
clearBypass(handlerType)Bypass a specific handler by type
Signature
public static void clearBypass(Type handlerType)
Parameters
handlerType
Type:
Type The class to be bypassed. Must end with ".class"
Example
clearBypass(handlerNames)Bypass a list of handlers
Signature
public static void clearBypass(List<String> handlerNames)
Parameters
handlerNames
Type:
List<String> List of Strings of handlers to bypass
Example
clearBypassList()
Clear the entire bypass list, but keep the global bypass flag intact
This is useful for resetting the list of handlers to bypass while maintaining global bypassing Signature
public static void clearBypassList()
Example
clearGlobalBypass()
Clear only the global bypass flag, leaving the list of bypassed handlers intact
This is useful for keeping a base set of bypassed handlers intact for an entire operation Signature
public static void clearGlobalBypass()
Example
clearMaxLoopCount()Removes the limit for the number of times we allow this class to run
Signature
public void clearMaxLoopCount()
debug()Instance method to show debug logs for just this trigger handler, allowing method chaining in the trigger.
Signature
public TriggerHandler debug()
Returns
TriggerHandler returning this class enables method chainingSee
Since
Example
debug(enabled)Instance method to show debug logs for just this trigger handler, allowing method chaining in the trigger.
Signature
public TriggerHandler debug(Boolean enabled)
Parameters
enabled
Type:
Boolean true to enable; false to disable
Returns
TriggerHandler returning this class enables method chainingSee
Since
Example
getHandlerName()
Get the name of the current handler. This can be set by using the constructor with the string parameter to improve performance
Signature
@TestVisible
private String getHandlerName()
Returns
String Name of the current handlerSee
TriggerHandler.handlerName
getLoopCount(handlerName)return the current loop count
Signature
public static Integer getLoopCount(String handlerName)
Parameters
handlerName
Type:
String The handler class to check for the current loop count
Returns
Integer How many times has this handler run?incrementCheckLoopCount()
Increment the loop count and check if we exceeded the max loop count.
Silently exit if we have exceeded it. (Log to System.debug) Signature
private Boolean incrementCheckLoopCount()
Returns
Boolean Should the trigger continue execution?Authors
isBypassed(handlerName)A handler is considered bypassed if it was bypassed, or all handlers have been bypassed
Signature
public static Boolean isBypassed(String handlerName)
Parameters
handlerName
Type:
String The class name of the handler we are checking is bypassed
Returns
Boolean Is this handler bypassed?Example
isBypassed(handlerType)A handler is considered bypassed if it was bypassed, or all handlers have been bypassed
Signature
public static Boolean isBypassed(Type handlerType)
Parameters
handlerType
Type:
Type The handler class we are checking is bypassed
Returns
Boolean Is this handler bypassed?Since
Example
limits()Instance method to show limits for just this trigger handler, allowing method chaining in the trigger.
Signature
public TriggerHandler limits()
Returns
TriggerHandler returning this class enables method chainingSee
Since
Example
limits(enabled)Instance method to show/hide limits for just this trigger handler, allowing method chaining in the trigger.
Signature
public TriggerHandler limits(Boolean enabled)
Parameters
enabled
Type:
Boolean true to enable; false to disable
Returns
TriggerHandler returning this class enables method chainingSee
Since
Example
run()Main method that will be called during execution
See the sample trigger for the best way to set up your handler Signature
public void run()
Authors
Example
setBypass(handlerName, desiredValue)Set bypass status to a specific value. Eliminates the need to know the current bypass status
Signature
public static void setBypass(String handlerName, Boolean desiredValue)
Parameters
handlerName
Type:
String The name of the TriggerHandler class
desiredValue
Type:
Boolean true to bypass, and false to run the handler/clear the bypass
Since
Author
Example
setMaxLoopCount(max)
Limit the number of times this handler can be run before it fails silently
Returning TriggerHandler enables method
chaining when used in a trigger Use this in the trigger or handler Signature
public TriggerHandler setMaxLoopCount(Integer max)
Parameters
max
Type:
Integer Naximum number of times
Returns
TriggerHandler enables method chainingAuthor
Example
setTriggerContext()Base method called by constructor to set the current context
Signature
@TestVisible
private void setTriggerContext()
setTriggerContext(opType, testMode)
Set the current trigger context based on the System.TriggerOperation
If we are not in a trigger context, then we set isTriggerExecuting to false <r>A single line is not covered by tests because we do not "fool" the handler into thinking that Trigger.isExecuting is true when we are running our tests. This line would be covered by any trigger handler in the org (and is
the reason we include a sample handler for assistance when creating a package for this project)
Signature
@TestVisible
private void setTriggerContext(System.TriggerOperation opType, Boolean testMode)
Parameters
opType
Type:
System.TriggerOperation The operation type - set automatically by the system
testMode
Type:
Boolean Only used in test methods to force certain contexts
See
TriggerHandler.isTriggerExecuting
showDebug()
Called in the trigger to force the class to debug trigger entry and exit with context.
Use this in the trigger or handler Signature
public static void showDebug()
See
Author
Since
Example
showDebug(enabled)
Called in the trigger to force the class to debug trigger entry and exit with context.
Set to true to show entry and exit. Signature
public static void showDebug(Boolean enabled)
Parameters
enabled
Type:
Boolean true to enable; false to disable
See
Author
Since
showLimits()Called before the trigger to force the class to debug query limits when it runs.
Use this in the trigger or handler Signature
public static void showLimits()
See
Example
showLimits(enabled)
Called before the trigger to enable the class to debug (or not) query limits when it runs.
Set to true to show limits. Use this in the trigger or handler Signature
public static void showLimits(Boolean enabled)
Parameters
enabled
Type:
Boolean true to enable; false to disable
See
validateRun()
Make sure this trigger should continue to run
Returning false causes trigger handler to exit
Signature
@TestVisible
private Boolean validateRun()
Returns
Boolean Is the run valid?Exceptions
TriggerHandlerException
See
TriggerHandler.LoopCountInner class for managing the loop count per handler
Signature
@TestVisible
private class LoopCount
TriggerHandler.LoopCount Properties
TriggerHandler.LoopCount Constructors
LoopCount(max)Constructor with specified max loops
Signature
public LoopCount(Integer max)
Parameters
max
Type:
Integer Max number of loops allowed
TriggerHandler.LoopCount Methods
exceeded()Determines if we're about to exceed the loop count.
Signature
public Boolean exceeded()
Returns
Boolean True if less than 0 or more than max.getCount()Returns the current loop count.
Signature
public Integer getCount()
Returns
Integer Current loop count.getMax()Returns the max loop count.
Signature
public Integer getMax()
Returns
Integer Max loop count.increment()Increment the internal counter returning the results of this.exceeded().
Signature
public Boolean increment()
Returns
Boolean true if count will exceed max count or is less than 0.setMax(max)Sets the max loop count
Signature
public void setMax(Integer max)
Parameters
max
Type:
Integer The integer to set max to.
TriggerHandler.TriggerHandlerExceptionException class
Signature
public class TriggerHandlerException extends Exception
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||