Skip to content

TRG_Base

Class · Group: Triggers

apex
global inherited sharing virtual class TRG_Base

Known Derived Types: TRG_ExecuteValidationRules

The base class for trigger actions, designed to be extended and implement relevant interfaces. This class provides the core functionality for executing trigger actions based on the trigger context, with support for bypassing specific SObject types. It is adapted from the apex-trigger-actions-framework.

Since: 1.0

Example:

apex
public with sharing class TRG_SetFoobarDefaults extends TRG_Base implements IF_Trigger.BeforeInsert
{
    public void beforeInsert(List<SObject> newRecords)
    {
        for(SObject record : newRecords)
        {
            if(record.get(Foobar__c.Name) == null)
            {
                record.put(Foobar__c.Name, 'Default');
            }
        }
    }
}

See Also: TRG_Dispatcher, IF_Trigger


Properties

PropertyDescription
global enum BypassTypeIndicates the type of trigger bypass being applied.
global TriggerOperation contextThe current operation type being processed by the trigger action.
global String sObjectNameThe API name of the SObject being processed by the trigger action.
global List triggerNewThe list of "new" SObjects in the trigger context.
global List triggerOldThe list of "old" SObjects in the trigger context.
global Map triggerOldMapMap of "old" SObjects keyed by Id, for efficient field change detection in update contexts.

Methods

MethodDescription
global static void bypass(SObjectType sObjectType)Bypasses trigger actions for the specified SObject type.
global static void bypass(String sObjectName)Bypasses trigger actions for the specified SObject type.
global static void bypass(String name, TRG_Base.BypassType type)Bypasses a trigger by name, dispatching to the correct bypass set based on the bypass type.
global static void bypassAction(String actionClassName)Bypasses a specific trigger action class by name.
global static void clearActionBypass(String actionClassName)Clears the bypass for a specific trigger action class, allowing it to execute.
global static void clearAllActionBypasses()Clears all action-level bypasses, allowing all trigger action classes to execute.
global static void clearAllBypasses()Clears all SObject type bypasses, allowing all trigger actions to execute.
global static void clearAllBypasses(TRG_Base.BypassType type)Clears all trigger bypasses, dispatching to the correct bypass set based on the bypass type.
global static void clearBypass(SObjectType sObjectType)Clears the bypass for the specified SObject type, allowing trigger actions to execute.
global static void clearBypass(String sObjectName)Clears the bypass for the specified SObject type, allowing trigger actions to execute.
global static void clearBypass(String name, TRG_Base.BypassType type)Clears a trigger bypass by name, dispatching to the correct bypass set based on the bypass type.
global static Boolean isActionBypassed(String actionClassName)Checks if a specific trigger action class is bypassed.
global static Boolean isBypassed(SObjectType sObjectType)Checks if trigger actions are bypassed for the specified SObject type.
global static Boolean isBypassed(String sObjectName)Checks if trigger actions are bypassed for the specified SObject type.
global static Boolean isBypassed(String name, TRG_Base.BypassType type)Checks if a trigger is bypassed, dispatching to the correct bypass set based on the bypass type.
global static TRG_Base.BypassType resolveBypassType(String bypassTypeString)Parses a bypass type string into the corresponding BypassType enum value.
global void run()Executes the relevant trigger action based on the implemented interface and trigger context.
global static void setBypassReason(String reason)Sets an optional transaction-scoped reason string that is attached to every subsequent bypass audit log entry.

Property Details

context

apex
@TestVisible global TriggerOperation context

Type: TriggerOperation

The current operation type being processed by the trigger action.

Since:

Example:

sObjectName

apex
@TestVisible global String sObjectName

Type: String

The API name of the SObject being processed by the trigger action.

Since:

Example:

triggerNew

apex
@TestVisible global List<SObject> triggerNew

Type: List

The list of "new" SObjects in the trigger context.

Since:

Example:

triggerOld

apex
@TestVisible global List<SObject> triggerOld

Type: List

The list of "old" SObjects in the trigger context.

Since:

Example:

triggerOldMap

apex
@TestVisible global Map<Id, SObject> triggerOldMap

Type: Map

Map of "old" SObjects keyed by Id, for efficient field change detection in update contexts.

Since:

Example:


Method Details

bypass

apex
global static void bypass(SObjectType sObjectType)

Bypasses trigger actions for the specified SObject type. Type-safe overload that accepts an SObjectType token instead of a string name.

Parameters:

  • sObjectType (SObjectType) - The SObjectType token of the object to bypass.

Since: 1.0

Example:

apex
TRG_Base.bypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // true

bypass

apex
global static void bypass(String sObjectName)

Bypasses trigger actions for the specified SObject type.

Parameters:

  • sObjectName (String) - The API name of the SObject type to bypass.

Since: 1.0

Example:

apex
TRG_Base.bypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // true

bypass

apex
global static void bypass(String name, TRG_Base.BypassType type)

Bypasses a trigger by name, dispatching to the correct bypass set based on the bypass type.

Parameters:

  • name (String) - The object API name or action class name to bypass
  • type (TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)

Since: 1.0

Example:

apex
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.bypass('TRG_SetDefaults', TRG_Base.BypassType.CLASS_NAME);

bypassAction

apex
global static void bypassAction(String actionClassName)

Bypasses a specific trigger action class by name.

Parameters:

  • actionClassName (String) - The class name of the trigger action to bypass.

Since: 1.0

Example:

apex
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // true

clearActionBypass

apex
global static void clearActionBypass(String actionClassName)

Clears the bypass for a specific trigger action class, allowing it to execute.

Parameters:

  • actionClassName (String) - The class name of the trigger action to clear from bypass.

Since: 1.0

Example:

apex
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
TRG_Base.clearActionBypass('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // false

clearAllActionBypasses

apex
global static void clearAllActionBypasses()

Clears all action-level bypasses, allowing all trigger action classes to execute.

Since: 1.0

Example:

apex
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
TRG_Base.bypassAction('TRG_ValidateFields');
TRG_Base.clearAllActionBypasses();
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // false

clearAllBypasses

apex
global static void clearAllBypasses()

Clears all SObject type bypasses, allowing all trigger actions to execute.

Since: 1.0

Example:

apex
TRG_Base.bypass('Account');
TRG_Base.bypass('Contact');
TRG_Base.clearAllBypasses();
Boolean isBypassed = TRG_Base.isBypassed('Account'); // false

clearAllBypasses

apex
global static void clearAllBypasses(TRG_Base.BypassType type)

Clears all trigger bypasses, dispatching to the correct bypass set based on the bypass type.

Parameters:

Since: 1.0

Example:

apex
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.clearAllBypasses(TRG_Base.BypassType.OBJECT_NAME);
Boolean isBypassed = TRG_Base.isBypassed('Account'); // false

clearBypass

apex
global static void clearBypass(SObjectType sObjectType)

Clears the bypass for the specified SObject type, allowing trigger actions to execute. Type-safe overload that accepts an SObjectType token instead of a string name.

Parameters:

  • sObjectType (SObjectType) - The SObjectType token of the object to clear from bypass.

Since: 1.0

Example:

apex
TRG_Base.bypass(Account.SObjectType);
TRG_Base.clearBypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // false

clearBypass

apex
global static void clearBypass(String sObjectName)

Clears the bypass for the specified SObject type, allowing trigger actions to execute.

Parameters:

  • sObjectName (String) - The API name of the SObject type to clear from bypass.

Since: 1.0

Example:

apex
TRG_Base.bypass('Account');
TRG_Base.clearBypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // false

clearBypass

apex
global static void clearBypass(String name, TRG_Base.BypassType type)

Clears a trigger bypass by name, dispatching to the correct bypass set based on the bypass type.

Parameters:

  • name (String) - The object API name or action class name to clear
  • type (TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)

Since: 1.0

Example:

apex
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.clearBypass('Account', TRG_Base.BypassType.OBJECT_NAME);
Boolean isBypassed = TRG_Base.isBypassed('Account'); // false

isActionBypassed

apex
global static Boolean isActionBypassed(String actionClassName)

Checks if a specific trigger action class is bypassed.

Parameters:

  • actionClassName (String) - The class name of the trigger action to check.

Returns: Boolean - Boolean True if the action class is bypassed, false otherwise.

Since: 1.0

Example:

apex
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // true

isBypassed

apex
global static Boolean isBypassed(SObjectType sObjectType)

Checks if trigger actions are bypassed for the specified SObject type. Type-safe overload that accepts an SObjectType token instead of a string name.

Parameters:

  • sObjectType (SObjectType) - The SObjectType token of the object to check.

Returns: Boolean - Boolean True if the SObject type is bypassed, false otherwise.

Since: 1.0

Example:

apex
TRG_Base.bypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // true

isBypassed

apex
global static Boolean isBypassed(String sObjectName)

Checks if trigger actions are bypassed for the specified SObject type.

Parameters:

  • sObjectName (String) - The API name of the SObject type to check.

Returns: Boolean - Boolean True if the SObject type is bypassed, false otherwise.

Since: 1.0

Example:

apex
TRG_Base.bypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // true

isBypassed

apex
global static Boolean isBypassed(String name, TRG_Base.BypassType type)

Checks if a trigger is bypassed, dispatching to the correct bypass set based on the bypass type.

Parameters:

  • name (String) - The object API name or action class name to check
  • type (TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)

Returns: Boolean - True if bypassed

Since: 1.0

Example:

apex
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
Boolean result = TRG_Base.isBypassed('Account', TRG_Base.BypassType.OBJECT_NAME); // true

resolveBypassType

apex
global static TRG_Base.BypassType resolveBypassType(String bypassTypeString)

Parses a bypass type string into the corresponding BypassType enum value. Wraps the standard valueOf call with a user-friendly error message on failure.

Parameters:

  • bypassTypeString (String) - The string to parse (must be 'OBJECT_NAME' or 'CLASS_NAME')

Returns: TRG_Base.BypassType - The parsed BypassType enum value

Throws:

Since: 1.0

Example:

apex
TRG_Base.BypassType type = TRG_Base.resolveBypassType('OBJECT_NAME');

run

apex
global void run()

Executes the relevant trigger action based on the implemented interface and trigger context. This method checks the trigger operation and calls the appropriate interface method if implemented.

Throws:

Since: 1.0

Example:

apex
public class CustomTriggerAction extends TRG_Base implements IF_Trigger.BeforeInsert
{
    public void beforeInsert(List<SObject> newRecords)
    {
        for (SObject record : newRecords)
        {
            Account accountRecord = (Account)record;
            accountRecord.Name = 'Updated Name';
        }
    }
}
// In a trigger context, calling run() will invoke beforeInsert for BEFORE_INSERT

setBypassReason

apex
global static void setBypassReason(String reason)

Sets an optional transaction-scoped reason string that is attached to every subsequent bypass audit log entry. Use before a batch of bypass calls to document the operational context (e.g., a data migration or incident response). Pass null or empty to clear.

Parameters:

  • reason (String) - The reason string to record, or null to clear the previously set reason.

Since: 1.0

Example:

apex
TRG_Base.setBypassReason('Customer data migration 2026-04-19');
TRG_Base.bypass(Account.SObjectType);
TRG_Base.bypass(Contact.SObjectType);