Retry Event Triggers with EventBus.RetryableException

Get another chance to process event notifications. Retrying a trigger is helpful when a transient error occurs or when waiting for a condition to change. Retry a trigger if the error or condition is external to the event records and is likely to go away later.

Where possible, we changed noninclusive terms to align with our company value of Equality. We maintained certain terms to avoid any effect on customer implementations.

Note

An example of a transient condition: A trigger adds a related record to a master record if a field on the master record equals a certain value. It’s possible that in a subsequent try, the field value changes and the trigger can perform the operation.

To retry the event trigger, throw EventBus.RetryableException. Events are resent after a small delay. The delay increases in subsequent retries. If the trigger receives a batch of events, retrying the trigger causes all events in the batch to be resent. The events are resent in their original order based on the ReplayID field values, which are unchanged. The trigger processes the resent events and later batches sequentially. Resent events have the same field values as the original events, but the batch sizes of the events can differ. For example, the initial trigger can receive events with replay ID 10 to 20. The resent batch can be larger, containing events with replay ID 10 to 40. When the trigger is retried, the DML operations performed in the trigger before the retry are rolled back and no changes are saved.

Limit the Number of Retry Attempts

You can run a trigger up to 10 times when it’s retried (the initial run plus 9 retries). After the trigger is retried 9 times, it moves to the error state and stops processing new events. Events sent after the trigger moves to the error state and before it returns to the running state aren’t resent to the trigger. To resume event processing, fix the trigger and save it.

We recommend limiting the retries to less than 9 times. Use the EventBus.TriggerContext.currentContext().retries property to check how many times the trigger has been retried. Alternatively, you can query the EventBusSubscriber.retries field in API version 43.0 and later.

For more code examples, see "Apply Best Practices for Writing Platform Event Triggers" in the Platform Events Debugging Trailhead module.

Note

Example

This example is a skeletal trigger that gives you an idea of how to throw EventBus.RetryableException and limit the number of retries. The trigger uses an if statement to check whether a certain condition is true. Alternatively, you can use a try-catch block and throw EventBus.RetryableException in the catch block.

1trigger ResendEventsTrigger on Low_Ink__e (after insert) {
2    if (condition == true) {        
3        // Process platform events.        
4    } else {
5        // Ensure we don't retry the trigger more than 4 times
6        if (EventBus.TriggerContext.currentContext().retries < 4) {
7            // Condition isn't met, so try again later.
8            throw new EventBus.RetryableException(
9                     'Condition is not met, so retrying the trigger again.');
10        } else {
11            // Trigger was retried enough times so give up and
12            // resort to alternative action.
13            // For example, send email to user.
14        }
15    }
16}