How do Platform Event triggers execute automated processing upon receiving published event messages?
Answer
Basics: Triggers on platform events let Salesforce itself react to a message, for example to update records or call an external system. Only the after insert context is supported, because events are not editable or deletable.
How it works:
- A publisher puts events on the bus.
- The platform delivers them to the trigger in batches (up to 2,000 messages by default per invocation).
- The trigger runs as the Automated Process user unless you configure another user with
PlatformEventSubscriberConfig. - The trigger runs in its own transaction with its own governor limits, and
Trigger.newholds the batch of events. - If it fails, you can throw
EventBus.RetryableExceptionfor the platform to redeliver the batch, up to a maximum number of retries. - The trigger tracks its position through a replay ID checkpoint. By default, an uncaught failure moves on.
Advanced / deep dive: A subscriber config lets you set the running user and a smaller batch size, which helps when each event triggers heavy work or callouts. You can also call EventBus.TriggerContext.currentContext().setResumeCheckpoint(replayId) to commit partial progress so a retry does not repeat work already done. Triggers should be idempotent, because redelivery and at-least-once delivery are possible. Keep the trigger thin and use a handler. A Flow subscribed to the event is the low-code alternative.
Code / config example:
trigger OrderEventTrigger on Order_Event__e (after insert) {
List<Task> tasks = new <Task>();
(Order_Event__e e : Trigger.new) {
tasks.add( (Subject = + e.Order_Id__c, Status = ));
EventBus.TriggerContext.currentContext().setResumeCheckpoint(e.ReplayId);
}
{
insert tasks;
} (DmlException ex) {
.RetryableException( + ex.getMessage());
}
}