TRG_Base
Class · Group: Triggers
global inherited sharing virtual class TRG_BaseKnown 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:
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
| Property | Description |
|---|---|
| global enum BypassType | Indicates the type of trigger bypass being applied. |
| global TriggerOperation context | The current operation type being processed by the trigger action. |
| global String sObjectName | The API name of the SObject being processed by the trigger action. |
| global List triggerNew | The list of "new" SObjects in the trigger context. |
| global List triggerOld | The list of "old" SObjects in the trigger context. |
| global Map triggerOldMap | Map of "old" SObjects keyed by Id, for efficient field change detection in update contexts. |
Methods
| Method | Description |
|---|---|
| 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
@TestVisible global TriggerOperation contextType: TriggerOperation
The current operation type being processed by the trigger action.
Since:
Example:
sObjectName
@TestVisible global String sObjectNameType: String
The API name of the SObject being processed by the trigger action.
Since:
Example:
triggerNew
@TestVisible global List<SObject> triggerNewType: List
The list of "new" SObjects in the trigger context.
Since:
Example:
triggerOld
@TestVisible global List<SObject> triggerOldType: List
The list of "old" SObjects in the trigger context.
Since:
Example:
triggerOldMap
@TestVisible global Map<Id, SObject> triggerOldMapType: Map
Map of "old" SObjects keyed by Id, for efficient field change detection in update contexts.
Since:
Example:
Method Details
bypass
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:
TRG_Base.bypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // truebypass
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:
TRG_Base.bypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // truebypass
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 bypasstype(TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)
Since: 1.0
Example:
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.bypass('TRG_SetDefaults', TRG_Base.BypassType.CLASS_NAME);bypassAction
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:
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // trueclearActionBypass
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:
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
TRG_Base.clearActionBypass('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // falseclearAllActionBypasses
global static void clearAllActionBypasses()Clears all action-level bypasses, allowing all trigger action classes to execute.
Since: 1.0
Example:
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
TRG_Base.bypassAction('TRG_ValidateFields');
TRG_Base.clearAllActionBypasses();
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // falseclearAllBypasses
global static void clearAllBypasses()Clears all SObject type bypasses, allowing all trigger actions to execute.
Since: 1.0
Example:
TRG_Base.bypass('Account');
TRG_Base.bypass('Contact');
TRG_Base.clearAllBypasses();
Boolean isBypassed = TRG_Base.isBypassed('Account'); // falseclearAllBypasses
global static void clearAllBypasses(TRG_Base.BypassType type)Clears all trigger bypasses, dispatching to the correct bypass set based on the bypass type.
Parameters:
type(TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)
Since: 1.0
Example:
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.clearAllBypasses(TRG_Base.BypassType.OBJECT_NAME);
Boolean isBypassed = TRG_Base.isBypassed('Account'); // falseclearBypass
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:
TRG_Base.bypass(Account.SObjectType);
TRG_Base.clearBypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // falseclearBypass
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:
TRG_Base.bypass('Account');
TRG_Base.clearBypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // falseclearBypass
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 cleartype(TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)
Since: 1.0
Example:
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
TRG_Base.clearBypass('Account', TRG_Base.BypassType.OBJECT_NAME);
Boolean isBypassed = TRG_Base.isBypassed('Account'); // falseisActionBypassed
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:
TRG_Base.bypassAction('TRG_SetFoobarDefaults');
Boolean isBypassed = TRG_Base.isActionBypassed('TRG_SetFoobarDefaults'); // trueisBypassed
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:
TRG_Base.bypass(Account.SObjectType);
Boolean isBypassed = TRG_Base.isBypassed(Account.SObjectType); // trueisBypassed
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:
TRG_Base.bypass('Account');
Boolean isBypassed = TRG_Base.isBypassed('Account'); // trueisBypassed
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 checktype(TRG_Base.BypassType) - The bypass type (OBJECT_NAME or CLASS_NAME)
Returns: Boolean - True if bypassed
Since: 1.0
Example:
TRG_Base.bypass('Account', TRG_Base.BypassType.OBJECT_NAME);
Boolean result = TRG_Base.isBypassed('Account', TRG_Base.BypassType.OBJECT_NAME); // trueresolveBypassType
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:
- System.Exception - If the string does not match a valid BypassType value
Since: 1.0
Example:
TRG_Base.BypassType type = TRG_Base.resolveBypassType('OBJECT_NAME');run
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:
- TypeException - If the class does not implement the required interface for the current context.
- UTIL_Exceptions.IllegalStateException - If called outside of a trigger execution context.
Since: 1.0
Example:
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_INSERTsetBypassReason
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:
TRG_Base.setBypassReason('Customer data migration 2026-04-19');
TRG_Base.bypass(Account.SObjectType);
TRG_Base.bypass(Contact.SObjectType);