Sub topic 5
Publishing events from flows
This chapter assumes you have defined a platform event such as Order_Shipped__e with its fields and publish behavior. It also assumes you have built a basic record-triggered flow. Flows publish events without Apex, which lets administrators feed the same event bus that your triggers and components subscribe to.
How a flow publishes an event
A flow publishes an event with a Create Records element. You choose the platform event object, such as Order_Shipped__e, and set its fields. When the flow runs the element, Salesforce publishes the event to the event bus. The element works in these flow types:
- Record-triggered flows that run after the save
- Autolaunched flows
- Screen flows
A before-save record-triggered flow can't create records, so it can't publish events.
Setting up the element
- Create a record-triggered flow on Order that runs after the record is saved. Add an entry condition, for example
Statusis changed toShipped. - Add a Create Records element and select OrderShipped_e as the object.
- Set
Order_Number__cfrom the triggering record's order number field. SetStatus__ctoShipped. - Save and activate the flow.
When an order moves to Shipped, the flow publishes one event. Any subscriber on the channel receives it, whether it is a trigger, another flow, or a component.
Publish behavior applies to flows
The publish behavior you set on the event definition controls when the flow's event leaves the transaction.
| Publish behavior | When the event is published | If the flow's transaction rolls back |
|---|---|---|
| Publish After Commit | After the transaction commits | The event is not published |
| Publish Immediately | When the Create Records element runs | The event is still published |
Choose Publish After Commit when subscribers should only react to changes that were saved. Choose Publish Immediately when you want the event to exist even if the transaction fails, for example for logging.
Handling failures
Flows don't return a SaveResult the way EventBus.publish does in Apex. To catch a failed publish, add a fault path to the Create Records element. Route it to an action that records the problem, such as creating a log record or sending an email.
A failure on the Create Records element can also roll back the rest of the flow's work, depending on where the flow runs. Test the fault path on purpose, for example by leaving out a required field.
Limits and constraints
- Events are immutable. A flow can create one but can't update or delete it afterward.
- Publishing from a flow counts against the same event publishing allocations as Apex and the REST API.
- A flow that runs for many records at once publishes many events. A bulk data load that triggers your flow can use up the hourly allocation quickly.
Flow or Apex?
Use a flow when the logic is simple: a condition and a few field mappings. Use Apex when you need the SaveResult, loops over complex data, or tight control over error handling. Both publish to the same bus, so subscribers can't tell which one sent an event.
Exercise
A record-triggered flow publishes an event with Publish After Commit, and a later element in the same flow throws an unhandled error that rolls back the transaction. Do subscribers receive the event? What changes if the event uses Publish Immediately?
Show answer
With Publish After Commit, subscribers do not receive the event, because the transaction never committed. With Publish Immediately, the event was published when the Create Records element ran, so subscribers still receive it even though the record changes were rolled back.