GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • View as Markdown
  • View llms.txt

This view

  • Original page
  • New address

GeezerVison is showing davidschach.com — Salesforce Trigger Handler code documentation

Salesforce Trigger Handler code documentation
Documentation for Trigger Framework
    Show: All   public    private    protected    testMethod

    TriggerHandler

    The base class of every trigger handler. A handler extends it. Code cannot create the base class itself.
    EmptyStatementBlock is suppressed for the empty virtual context methods. A handler overrides only the methods that it needs. Stock PMD reports empty virtual methods, so this suppression keeps a scan with default rules free of findings.
    Signature
    public inherited sharing abstract class TriggerHandler
    See
    License, GitHub, TriggerHandlerTest
    Author
    Kevin OHara
    Since
  • 1.0.0
  • Author
    David Schach
    Since
  • 1.0.0
  • 1.1.0 Add andFinally()
  • 1.2.0 Add method chaining to setMaxLoopCount(), limits(), and debug()
  • 2.0.0 Add a constructor with the handler type, a loop limit for each trigger operation, and the debug settings of 1 handler
  • 2.0.0 Keep the named bypasses and the global bypass independent
  • 2.1.0 Bypass all handlers for a user with the BypassTriggerHandlers custom permission
  • 2.1.0 Add hideAllDebug() and showAllDebug(), the Set overloads of bypass() and clearBypass(), getLoopCount() by type, and a chainable clearMaxLoopCount()
  • TriggerHandler Properties

    Name Signature Annotations Description
    BYPASS_PERMISSION
    private static final String BYPASS_PERMISSION
    @TestVisible
    The API name of the custom permission that bypasses all handlers for the user who holds it. The framework ships the permission in force-app.
    bypasses
    private static BypassRegistry bypasses
    The bypasses of the transaction: the named bypasses and the global bypass.
    CLASS_NAME_END
    private static final String CLASS_NAME_END
    The character after the class name in the toString() text of an Apex object.
    doHideAllDebug
    private static Boolean doHideAllDebug
    true when no handler writes the entry and exit lines. It wins over doShowDebug and over the setting of each handler. hideAllDebug(), hideAllDebug(Boolean), and showAllDebug() set it.
    doShowDebug
    private static Boolean doShowDebug
    @TestVisible
    true when all handlers write the entry and exit lines of a run. showDebug() and showDebug(Boolean) set it, and so do the deprecated debug() and debug(Boolean).
    doShowLimits
    private static Boolean doShowLimits
    @TestVisible
    true when all handlers write the limits line after a run. showLimits() and showLimits(Boolean) set it, and so do the deprecated limits() and limits(Boolean).
    ERROR_BYPASS_VALUE
    private static final String ERROR_BYPASS_VALUE
    @TestVisible
    The error text when a bypass value is null.
    ERROR_HANDLER_NAME
    private static final String ERROR_HANDLER_NAME
    @TestVisible
    The error text when a handler name is null or blank.
    ERROR_HANDLER_NAMES
    private static final String ERROR_HANDLER_NAMES
    @TestVisible
    The error text when a list of handler names is null.
    ERROR_HANDLER_TYPE
    private static final String ERROR_HANDLER_TYPE
    @TestVisible
    The error text when a handler type is null.
    ERROR_LOOP_COUNT
    private static final String ERROR_LOOP_COUNT
    @TestVisible
    The error text when a maximum loop count is null.
    ERROR_TRIGGER_OPERATION
    private static final String ERROR_TRIGGER_OPERATION
    @TestVisible
    The error text when a trigger operation is null.
    ERROR_TRIGGERCONTEXT
    private static final String ERROR_TRIGGERCONTEXT
    @TestVisible
    The error text, in English, when run() is called outside a trigger.
    handlerName
    private String handlerName
    The name of this handler. The Type constructor sets it. Without the Type constructor, getHandlerName() finds the name when it is first needed.
    handlerShowDebug
    private Boolean handlerShowDebug
    The debug setting of this handler only: true, false, or null. With null, the handler uses doShowDebug. showHandlerDebug() and showHandlerDebug(Boolean) set it. doHideAllDebug wins over it.
    handlerShowLimits
    private Boolean handlerShowLimits
    The limits setting of this handler only: true, false, or null. With null, the handler uses doShowLimits. showHandlerLimits() and showHandlerLimits(Boolean) set it.
    isTriggerExecuting
    private Boolean isTriggerExecuting
    @TestVisible
    true when the handler runs in a trigger, or in a test context from setTestContext().
    loopCounters
    private static LoopCounterRegistry loopCounters
    The loop counters of all handlers in the transaction. A trigger operation has a counter after the first call of setMaxLoopCount() for it, also with 0 or a negative value. Without a counter, getLoopCount() returns NOT_COUNTED.
    NO_LOOP_LIMIT
    private static final Integer NO_LOOP_LIMIT
    The maximum that means no limit. clearMaxLoopCount() sets it, and a LoopCounter stores a maximum of 0 or less as this value.
    NOT_COUNTED
    private static final Integer NOT_COUNTED
    @TestVisible
    The value that getLoopCount() returns when setMaxLoopCount() was never called for the operation. The framework does not count those runs.
    triggerEvent
    private System.TriggerOperation triggerEvent
    @TestVisible
    The trigger operation of the current run. In a test, setTestContext() sets it.

    TriggerHandler Constructors

    • TriggerHandler ()
      Creates a handler. The handler name comes from the runtime class name when the framework first needs it.
      That lookup serializes all fields of the handler. To prevent the lookup, pass the handler type to the Type constructor.
      For an inner class, the lookup gives only the short name, which does not match bypass(Type). An inner-class handler must use the Type constructor.
    • TriggerHandler (handlerType)
      Creates a handler with its type. The handler name is Type.getName(), so it always matches bypass(Type), also for an inner class. The framework does no name lookup.
      It replaces the String constructor TriggerHandler(String handlerName), which version 2.0 removed. To upgrade a handler, change super('ClassName') to super(ClassName.class).

    TriggerHandler()

    Creates a handler. The handler name comes from the runtime class name when the framework first needs it.
    That lookup serializes all fields of the handler. To prevent the lookup, pass the handler type to the Type constructor.
    For an inner class, the lookup gives only the short name, which does not match bypass(Type). An inner-class handler must use the Type constructor.
    Signature
    public TriggerHandler()
    See
    TriggerHandler.TriggerHandler
    Since
  • 1.0.0
  • Example
     // in the trigger 
     new OpportunityTriggerHandler().run(); 
     // in the handler class 
     public OpportunityTriggerHandler() { 
       super(); 
       this.newRecords = (List<Opportunity>) Trigger.new; 
     }

    TriggerHandler(handlerType)

    Creates a handler with its type. The handler name is Type.getName(), so it always matches bypass(Type), also for an inner class. The framework does no name lookup.
    It replaces the String constructor TriggerHandler(String handlerName), which version 2.0 removed. To upgrade a handler, change super('ClassName') to super(ClassName.class).
    Signature
    protected TriggerHandler(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.
    See
    TriggerHandler.TriggerHandler
    Since
  • 2.0.0
  • Example
     // in the trigger 
     new AccountSampleTriggerHandler().run(); 
     // in the handler class 
     public AccountSampleTriggerHandler() { 
       super(AccountSampleTriggerHandler.class); 
       this.newRecords = (List<Account>) Trigger.new; 
       this.oldRecordsMap = (Map<Id, Account>) Trigger.oldMap; 
     } 
     // in an inner-class handler: the outer class name, a dot, and the inner class name 
     public OpportunityTriggerHandler() { 
       super(SalesHandlers.OpportunityTriggerHandler.class); 
     }

    TriggerHandler Methods

    • 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.

    afterDelete()

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

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

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

    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
  • 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.
    Signature
    protected virtual void andFinally()
    See
    The Joys of Apex
    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)); 
       } 
     }

    beforeDelete()

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

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

    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
  • bypass(handlerName)

    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');

    bypass(handlerType)

    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
    vr8hub
    Since
  • 1.0.0
  • Example
     TriggerHandler.bypass(AccountSampleTriggerHandler.class);

    bypass(handlerNames)

    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' });

    bypass(handlerNames)

    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' });

    bypassAll()

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

    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).
    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 
     }

    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().
    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');

    clearAllBypasses()

    Clears all bypasses: the global bypass and all named bypasses.
    Signature
    public static void clearAllBypasses()
    Since
  • 1.0.0
  • Example
     TriggerHandler.clearAllBypasses();

    clearBypass(handlerName)

    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
    vr8hub
    Since
  • 1.0.0
  • Example
     TriggerHandler.clearBypass('AccountSampleTriggerHandler');

    clearBypass(handlerType)

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

    clearBypass(handlerNames)

    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' });

    clearBypass(handlerNames)

    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' });

    clearBypassList()

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

    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.
    Signature
    public static void clearGlobalBypass()
    Since
  • 1.0.0
  • Example
     TriggerHandler.clearGlobalBypass();

    clearMaxLoopCount()

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

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

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

    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.
    Signature
    @TestVisible
    protected String getHandlerName()
    Returns
    String The name of this handler.
    Since
  • 1.4.0
  • 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.
    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');

    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.
    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);

    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.
    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);

    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.
    Signature
    protected System.TriggerOperation getTriggerEvent()
    Returns
    System.TriggerOperation The current operation, or null outside a trigger.
    Since
  • 1.4.0
  • 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.
    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 
     }

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

    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.
    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);

    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().
    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
    David Schach, fbouzeraa
    Since
  • 1.0.0
  • 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().
    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 
     }

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

    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.
    Signature
    private Boolean isDebugOn()
    Returns
    Boolean true when this handler writes the entry and exit lines.
    Since
  • 2.0.0
  • isGlobalBypassed()

    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 
     }

    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.
    Signature
    private Boolean isLimitsOn()
    Returns
    Boolean true when this handler writes the limits line.
    Since
  • 2.0.0
  • 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.
    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 
     }

    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.
    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 
     }

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

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

    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.
    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); 
     }

    requireHandlerName(handlerName)

    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
  • requireHandlerNames(handlerNames)

    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
  • requireHandlerType(handlerType)

    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
  • 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.
    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
    Kevin OHara, David Schach
    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(); 
     }

    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.
    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
    David Schach
    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);

    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.
    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);

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

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

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

    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().
    Signature
    private void setTriggerContext()
    Since
  • 1.0.0
  • 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.
    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();

    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.
    Signature
    public static void showDebug()
    See
    TriggerHandler.showDebug
    Author
    David Schach
    Since
  • 1.0.0
  • Example
     TriggerHandler.showDebug(); // in the trigger, before the handler runs 
     new AccountSampleTriggerHandler().run();

    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.
    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
    David Schach
    Since
  • 1.0.0
  • Example
     TriggerHandler.showDebug(false);

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

    showHandlerDebug(enabled)

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

    showHandlerLimits()

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

    showHandlerLimits(enabled)

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

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

    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.
    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);

    validateRun()

    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
  • TriggerHandler.BypassRegistry

    Holds the bypasses of the transaction: the named bypasses and the global bypass. The 2 are independent.
    TriggerHandler keeps 1 instance in bypasses, because an inner class cannot have static members. The public static methods check their arguments and then call it.
    Signature
    @TestVisible
    private inherited sharing class BypassRegistry
    Author
    David Schach
    Since
  • 2.0.0
  • TriggerHandler.BypassRegistry Properties

    Name Signature Description
    allBypassed
    private Boolean allBypassed
    true when all handlers are bypassed.
    names
    private Set<String> names
    All handlers that are bypassed by name or by type, as the caller wrote them.
    namesLower
    private Set<String> namesLower
    A lower-case copy of names. All membership checks use it, so a name matches in any case.

    TriggerHandler.BypassRegistry Methods

    • add (handlerName)
      Bypasses a handler by name.
    • clearNames ()
      Clears all named bypasses. The global bypass stays.
    • contains (handlerName)
      Tells if a handler is bypassed by name, in any case.
    • hasNames ()
      Tells if 1 or more handlers are bypassed by name.
    • isAllBypassed ()
      Tells if all handlers are bypassed.
    • nameSet ()
      Returns the bypassed names, as the caller wrote them.
    • remove (handlerName)
      Clears the bypass of a handler by name, in any case. A name that is not bypassed changes nothing.
    • setAllBypassed (value)
      Sets the global bypass. The named bypasses stay.
    • sortedNames ()
      Returns the bypassed names in sorted order, as the caller wrote them.

    add(handlerName)

    Bypasses a handler by name.
    Signature
    public void add(String handlerName)
    Parameters
    handlerName
    Type: String
    The name. It is not null or blank.
    Since
  • 2.0.0
  • clearNames()

    Clears all named bypasses. The global bypass stays.
    Signature
    public void clearNames()
    Since
  • 2.0.0
  • contains(handlerName)

    Tells if a handler is bypassed by name, in any case.
    Signature
    public Boolean contains(String handlerName)
    Parameters
    handlerName
    Type: String
    The name. It is not null or blank.
    Returns
    Boolean true when the name is bypassed.
    Since
  • 2.0.0
  • hasNames()

    Tells if 1 or more handlers are bypassed by name.
    Signature
    public Boolean hasNames()
    Returns
    Boolean true when 1 or more names are bypassed.
    Since
  • 2.0.0
  • isAllBypassed()

    Tells if all handlers are bypassed.
    Signature
    public Boolean isAllBypassed()
    Returns
    Boolean true when the global bypass is on.
    Since
  • 2.0.0
  • nameSet()

    Returns the bypassed names, as the caller wrote them.
    Signature
    public Set<String> nameSet()
    Returns
    Set<String> A new set.
    Since
  • 2.0.0
  • remove(handlerName)

    Clears the bypass of a handler by name, in any case. A name that is not bypassed changes nothing.
    Signature
    public void remove(String handlerName)
    Parameters
    handlerName
    Type: String
    The name. It is not null or blank.
    Since
  • 2.0.0
  • setAllBypassed(value)

    Sets the global bypass. The named bypasses stay.
    Signature
    public void setAllBypassed(Boolean value)
    Parameters
    value
    Type: Boolean
    true to bypass all handlers, false to clear the global bypass.
    Since
  • 2.0.0
  • sortedNames()

    Returns the bypassed names in sorted order, as the caller wrote them.
    Signature
    public List<String> sortedNames()
    Returns
    List<String> A new sorted list.
    Since
  • 2.0.0
  • TriggerHandler.LoopCounter

    Counts the runs of 1 trigger operation of 1 handler, and compares the count with a maximum.
    Signature
    @TestVisible
    private inherited sharing class LoopCounter
    Author
    David Schach
    Since
  • 1.0.0 Add the class as LoopCount
  • 1.5.1 Rename LoopCount to LoopCounter
  • TriggerHandler.LoopCounter Properties

    Name Signature Description
    loopCount
    private Integer loopCount
    The number of runs so far, with the runs that the limit stopped.
    max
    private Integer max
    The maximum number of runs, or NO_LOOP_LIMIT for no limit.

    TriggerHandler.LoopCounter Constructors

    • LoopCounter (maxCount)
      Creates a counter with a maximum. The count starts at 0.

    LoopCounter(maxCount)

    Creates a counter with a maximum. The count starts at 0.
    Signature
    public LoopCounter(Integer maxCount)
    Parameters
    maxCount
    Type: Integer
    The maximum number of runs. 0 or a negative value means no limit.
    Since
  • 1.5.1
  • TriggerHandler.LoopCounter Methods

    • exceeded ()
      Tells if the count passed the limit.
    • getCount ()
      Returns the number of runs so far, with the runs that the limit stopped.
    • getMax ()
      Returns the maximum number of runs.
    • increment ()
      Adds 1 to the count and returns exceeded().
    • setMax (maxCount)
      Sets the maximum number of runs. The count does not change. A maximum of 0 or less becomes NO_LOOP_LIMIT.

    exceeded()

    Tells if the count passed the limit.
    Signature
    public Boolean exceeded()
    Returns
    Boolean true when the counter has a limit and the count is more than the limit.
    Since
  • 1.5.1
  • getCount()

    Returns the number of runs so far, with the runs that the limit stopped.
    Signature
    public Integer getCount()
    Returns
    Integer The count.
    Since
  • 1.5.1
  • getMax()

    Returns the maximum number of runs.
    Signature
    public Integer getMax()
    Returns
    Integer The maximum, or -1 when there is no limit.
    Since
  • 1.5.1
  • increment()

    Adds 1 to the count and returns exceeded().
    Signature
    public Boolean increment()
    Returns
    Boolean true when the new count is more than the maximum. A counter with no limit never passes it.
    Since
  • 1.5.1
  • setMax(maxCount)

    Sets the maximum number of runs. The count does not change. A maximum of 0 or less becomes NO_LOOP_LIMIT.
    Signature
    public void setMax(Integer maxCount)
    Parameters
    maxCount
    Type: Integer
    The new maximum. 0 or a negative value means no limit.
    Since
  • 1.5.1
  • TriggerHandler.LoopCounterRegistry

    Holds the loop counters of the transaction, by handler name and then by trigger operation.
    TriggerHandler keeps 1 instance in loopCounters, because an inner class cannot have static members. The public methods check their arguments and then call it.
    Signature
    @TestVisible
    private inherited sharing class LoopCounterRegistry
    Author
    David Schach
    Since
  • 2.0.0
  • TriggerHandler.LoopCounterRegistry Properties

    Name Signature Description
    counters
    private Map<String, Map<System.TriggerOperation, LoopCounter>> counters
    The loop counters of each handler, by handler name and then by trigger operation.

    TriggerHandler.LoopCounterRegistry Methods

    • clearMax (handlerName)
      Removes the limit of all counters of a handler. The counters keep their counts. It creates no counter.
    • count (handlerName, operation)
      Returns the count of 1 trigger operation of a handler.
    • get (handlerName, operation)
      Returns the counter of 1 trigger operation of a handler.
    • isEmpty ()
      Tells if no handler has a counter. Then run() does not look up the handler name.
    • setMax (handlerName, operation, max)
      Sets the limit of 1 trigger operation of a handler. It creates the counter when the operation has none.
    • total (handlerName)
      Returns the total count of all trigger operations of a handler.

    clearMax(handlerName)

    Removes the limit of all counters of a handler. The counters keep their counts. It creates no counter.
    Signature
    public void clearMax(String handlerName)
    Parameters
    handlerName
    Type: String
    The name of the handler.
    Since
  • 2.0.0
  • count(handlerName, operation)

    Returns the count of 1 trigger operation of a handler.
    Signature
    public Integer count(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 NOT_COUNTED when setMaxLoopCount() was never called for the operation.
    Since
  • 2.0.0
  • get(handlerName, operation)

    Returns the counter of 1 trigger operation of a handler.
    Signature
    public LoopCounter get(String handlerName, System.TriggerOperation operation)
    Parameters
    handlerName
    Type: String
    The name of the handler.
    operation
    Type: System.TriggerOperation
    The trigger operation.
    Returns
    LoopCounter The counter, or null when setMaxLoopCount() was never called for the operation.
    Since
  • 2.0.0
  • isEmpty()

    Tells if no handler has a counter. Then run() does not look up the handler name.
    Signature
    public Boolean isEmpty()
    Returns
    Boolean true when no handler called setMaxLoopCount() in the transaction.
    Since
  • 2.0.0
  • setMax(handlerName, operation, max)

    Sets the limit of 1 trigger operation of a handler. It creates the counter when the operation has none.
    Signature
    public void setMax(String handlerName, System.TriggerOperation operation, Integer max)
    Parameters
    handlerName
    Type: String
    The name of the handler.
    operation
    Type: System.TriggerOperation
    The trigger operation.
    max
    Type: Integer
    The maximum number of runs. 0 or a negative value means no limit.
    Since
  • 2.0.0
  • total(handlerName)

    Returns the total count of all trigger operations of a handler.
    Signature
    public Integer total(String handlerName)
    Parameters
    handlerName
    Type: String
    The name of the handler.
    Returns
    Integer The total, or NOT_COUNTED when setMaxLoopCount() was never called for the handler.
    Since
  • 2.0.0
  • TriggerHandler.TriggerHandlerException

    The exception that the framework throws for a wrong call.
    Signature
    public class TriggerHandlerException extends Exception
    Author
    David Schach
    Since
  • 1.0.0
  • Powered By ApexDox VS Code
    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.