# How do Platform Event triggers execute automated processing upon receiving published event messages?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Event Triggers, Asynchronous Processing

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. A publisher puts events on the bus.
2. The platform delivers them to the trigger in batches (up to 2,000 messages by default per invocation).
3. The trigger runs as the Automated Process user unless you configure another user with `PlatformEventSubscriberConfig`.
4. The trigger runs in its own transaction with its own governor limits, and `Trigger.new` holds the batch of events.
5. If it fails, you can throw `EventBus.RetryableException` for the platform to redeliver the batch, up to a maximum number of retries.
6. 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:**
```apex
trigger OrderEventTrigger on Order_Event__e (after insert) {
    List<Task> tasks = new List<Task>();
    for (Order_Event__e e : Trigger.new) {
        tasks.add(new Task(Subject = 'Review order ' + e.Order_Id__c, Status = 'Not Started'));
        // Remember the last replay ID processed
        EventBus.TriggerContext.currentContext().setResumeCheckpoint(e.ReplayId);
    }
    try {
        insert tasks;
    } catch (DmlException ex) {
        throw new EventBus.RetryableException('Retry: ' + ex.getMessage());
    }
}
```

**Hidden gotchas & must-remember points:**
- Only `after insert` is supported. There is no `before` context and no update trigger.
- The running user is Automated Process by default, so `CreatedBy` on records and sharing behave differently from the publisher's context.
- Retries are limited (verify the current count), after which the batch is skipped.
- A callout in a trigger needs a `@future` or Queueable approach, since triggers cannot call out directly.
- Debug logs for the trigger need a trace flag on the Automated Process user.
- Event trigger processing is not guaranteed to be instant.

**Best practice:** Keep the handler idempotent, set a suitable batch size, and use resume checkpoints for large batches.

---
Source: ForceChallenges · last updated 2026-11-02
