TriggerContext Class

Provides information about the platform event or change event trigger that’s currently executing, such as how many times the trigger was retried due to the EventBus.RetryableException. Also, provides a method to resume trigger executions.

Namespace

EventBus

TriggerContext Properties

The following are properties for TriggerContext.

lastError

Read-only. The error message that the last thrown EventBus.RetryableException contains.

Signature

public String lastError {get;}

Property Value

Type: String

Usage

The error message that this property returns is the message that was passed in when creating the EventBus.RetryableException exception, as follows.

throw new EventBus.RetryableException(
             'Condition is not met, so retrying the trigger again.');

retries

Read-only. The number of times the trigger was retried due to throwing the EventBus.RetryableException.

Signature

public Integer retries {get;}

Property Value

Type: Integer

TriggerContext Methods

The following are methods for TriggerContext.

currentContext()

Returns an instance of the EventBus.TriggerContext class containing information about the currently executing trigger.

Signature

public static eventbus.TriggerContext currentContext()

Return Value

Type: EventBus.TriggerContext

Information about the currently executing trigger.

getResumeCheckpoint()

Returns the replay ID that was set by setResumeCheckpoint(). The returned value is the replay ID of the event message after which trigger processing resumes in a new trigger invocation.

Signature

public String getResumeCheckpoint()

Return Value

Type: String

setResumeCheckpoint(resumeReplayId)

Sets a checkpoint in the event stream where the platform event trigger resumes execution in a new invocation. Use this method to recover from limit and uncaught exceptions, or to control the number of events processed in one trigger execution. When calling this method, pass in the replay ID of the last successfully processed event message. When the trigger stops execution before all events in Trigger.New are processed, either because of an uncaught exception or intentionally, the trigger is invoked again. The new execution starts with the event message in the stream after the one with the checkpointed Replay ID.

Signature

public void setResumeCheckpoint(String resumeReplayId)

Parameters

resumeReplayId
Type: String
The replay ID of the last successfully processed platform event message, after which to resume processing in a new trigger execution context.

Return Value

Type: void

Usage

The method throws an EventBus.InvalidReplayIdException if the supplied Replay ID is not valid—the replay ID is not in the current trigger batch of events, in the Trigger.new list.

Example

This snippet shows how to call the method and pass in the replayId property of an event instance.

EventBus.TriggerContext.currentContext().setResumeCheckpoint(event.replayId);