Sub topic 8
Subscribing with a platform event-triggered flow
This chapter assumes you can define a platform event (such as Order_Shipped__e) and publish it. It also assumes you know basic Flow Builder concepts: elements, variables, and the difference between record-triggered and autolaunched flows. A platform event-triggered flow lets an admin subscribe to an event without writing Apex.
How the flow subscribes
A platform event-triggered flow is a flow whose start element listens to one platform event object. Each time an event is delivered, Salesforce starts a flow interview. An interview is one running instance of a flow. The event record is available in the flow as $Record.
This is the declarative counterpart of the Apex trigger from the previous chapter. Both receive the same events from the event bus. The difference is where the logic lives.
Building the flow
Follow these steps in Setup, under Flows.
- Click New Flow and choose Platform Event-Triggered Flow.
- In the start element, choose the event object. For this example, select
Order_Shipped__e. - Add a Create Records element. Create a Task.
- Set
Subjectto a formula such as"Follow up on order " & {!$Record.Order_Number__c}. - Set
StatustoNot Started. - Save the flow, give it a clear name, and activate it.
This produces the same result as OrderShippedTrigger: one Task for each shipped order.
Choosing the running user
An Apex trigger on a platform event runs as the Automated Process user by default. A flow gives you a choice in the start element's advanced options:
| Option | Runs as | Use it when |
|---|---|---|
| Automated Process user | The system user for the org | The work is system-level and does not depend on a person |
| A specific user | The user you select | You need predictable record ownership or permissions |
The running user's permissions decide what the flow can create or update. If a Create Records element fails with an access error, check this setting first.
Batching and bulk behavior
The event bus can deliver events in batches, but a flow still handles them one at a time. Each interview receives one event. Salesforce may bulkify the database work across interviews in the same batch, so avoid loops that contain Create Records, Update Records, or Get Records elements. Put DML outside loops and use collection variables when you build lists.
Common errors
- The flow never runs. Check that the flow is active, not only saved. Then confirm the event was published successfully.
- The flow fails on one event. Failed interviews appear in Setup under Paused and Failed Flow Interviews, or in the flow error emails. Fix the cause and publish a new event, since the flow does not retry the failed event the way a trigger can.
- Permission errors. The running user lacks access to the object or field the flow touches.
- Unexpected duplicates. If both an Apex trigger and a flow subscribe to the same event, each one runs. Keep one subscriber per purpose unless you want both actions.
Flow or trigger?
Choose a flow when the logic is simple, such as creating or updating records, sending an email, or calling a subflow, and an admin will maintain it. Choose a trigger when you need retries with checkpoints, complex branching, or code that is easier to test and review in source control.
Exercise
A platform event-triggered flow on Order_Shipped__e creates a Task for each event, but some events fail with an access error on the Task object. Name the setting you would inspect first and one way to fix it.
Show answer
Inspect the running user in the start element's advanced options. The Automated Process user may lack access to Task. Fix it by running the flow as a specific user who has the needed permissions, or by granting the needed access to the user the flow runs as.
Exercise
Why should a Create Records element not sit inside a loop in a platform event-triggered flow?
Show answer
Each pass through the loop would perform its own DML operation, which can reach governor limits. Add the new records to a collection variable inside the loop and create them with one Create Records element after the loop.