Force-Framework

Mark Brennand

TriggerV1

global with sharing class TriggerV1
DescriptionCopyright (c) 2025 Mark Brennand, released under MIT License.

Class defining the Trigger API available for public use.

The Subscriber interface defines the contract that implementations must provide to process events generated by the Trigger API.

Each Subscriber implementation must register itself with a Trigger metadata record.

See README for full details of the Trigger API.
AuthorMark Brennand

  Properties

Operation
global enum Operation
The type of trigger operation that has fired.

  Constants

ALL_OPERATIONS
global static final Set<Operation> ALL_OPERATIONS
Definition a subscriber may use to indicate that it wishes to subscribe to all the trigger operations.

Methods

  disable

global static void disable(final SObjectType type)
DescriptionDisables the subscribers for the given object type.
Parametertype: The object type whose subscribers are to be disabled.

  disable

global static void disable(Type type)
DescriptionDisables the given Subscriber class.

No further events will be published to the associated subscriber.
Parametertype: The Subscriber class to disable.

  enable

global static void enable(final SObjectType type)
DescriptionEnables the subscribers for the given object type.
Parametertype: The object type whose subscribers are to be enabled.

  enable

global static void enable(Type type)
DescriptionEnables the given Subscriber class.

Events will be published to the associated subscriber.
Parametertype: The Subscriber class to enable.

  isEnabled

global static Boolean isEnabled(final SObjectType type)
DescriptionDetermines whether subscribers are enabled for the given object type.
Parametertype: The object type whose subscription status is to be checked.
ReturnsTrue if the subscribers are enabled.

  isEnabled

global static Boolean isEnabled(Type type)
DescriptionDetermines whether the given Subscriber class is enabled.
Parametertype: The Subscriber class to be checked.
ReturnsTrue if the Subscriber class is enabled.

  publish

global static void publish()
DescriptionEntry point for Trigger API.

This method must be called from the Apex Trigger.

An event representing the Trigger action is created and published to all the subscribers bound to the type of object associated with the Apex Trigger.

TriggerV1.APIException

global with sharing class APIException extends Exception
DescriptionException thrown when a trigger operation fails.

TriggerV1.Event

global interface Event
DescriptionEvent created by the trigger to represent the operation being performed.

The event is sent to all the Subscribers bound to the object.

Methods

  getOldValue

SObject getOldValue(SObject record)
DescriptionGets the old value of a record.
Parameterrecord: The record whose old value is required.
ReturnsThe old value of the record, or null if no old record found.

  getOldValues

List<SObject> getOldValues()
DescriptionGets all the old values registered.
ReturnsAll the old values.

  getOperation

Operation getOperation()
DescriptionYields the trigger operation being performed.
ReturnsThe operation being performed by the trigger.

  getRecords

List<SObject> getRecords()
DescriptionYields the records passed to the trigger for processing.

Any records passed to an after trigger will have been cloned. This allows DML to be performed on them.
ReturnsThe records the trigger is processing.

  getRecursiveDepth

Integer getRecursiveDepth()
DescriptionYields the maximum recursive depth for the trigger.

The recursive depth is the number of times the trigger has been called as a result of DML performed on the object associated with the trigger in trigger logic.

If the recursive depth returned is 0, no checks on the recursive depth are performed.
ReturnsThe maximum recursive depth for the trigger.

  hasOldValue

Boolean hasOldValue(SObject record)
DescriptionChecks whether given record has an old value assigned.
Parameterrecord: The record whose old value is required.
ReturnsTrue if the record has an old value.

  isAfter

Boolean isAfter()
DescriptionIndicates whether the trigger is performing an after action.
ReturnsTrue if the trigger is performing an after action.

  isBefore

Boolean isBefore()
DescriptionIndicates whether the trigger is performing a before action.
ReturnsTrue if the trigger is performing a before action.

TriggerV1.Subscriber

global interface Subscriber
DescriptionClasses implementing this interface may register themselves for use of the Trigger API.

Methods

  forOperations

Set<Operation> forOperations()
DescriptionReturns the operations the subscriber will accept.
ReturnsThe operations.

  onEvent

void onEvent(Event event)
DescriptionMethod called by the Trigger API when a trigger fires.

The event argument encapsulates all the information for the trigger operation that has fired.

All the Subscriber implementations bound to the object whose trigger hss fired will have their onEvent method called.
Parameterevent: The trigger event.