Sub topic 5
Publishing events from flows
This chapter covers how to publish a platform event from a flow, using the Create Records element and the Salesforce Flow publishing behavior. It assumes you can already define a platform event with custom fields, and that you know the basics of Flow Builder: variables, elements, and the difference between record-triggered and screen flows. It also assumes you know what a transaction is and that governor limits apply to it.
Why publish from a flow
Publishing from Apex gives you the most control. Many teams, though, want administrators to publish events without writing code. A flow can do this. You can publish an event when a record changes, when a user finishes a screen, or on a schedule. Any subscriber can then react: an Apex trigger, another flow, a Lightning Web Component, or an external system.
A flow does not call EventBus.publish. A flow publishes an event by creating a record of the event object. That is the same idea as the REST API approach later in this tutorial: a platform event is an sObject, and inserting one publishes it.
Using the Create Records element
The standard way to publish from a flow is the Create Records element. The steps are the same whether the flow is record-triggered, screen, scheduled, or autolaunched.
- Add a Create Records element to the flow.
- Choose Use separate resources, and literal values to set the fields. Choose the event object, for example
Order_Shipped__e, as the object to create. - Set the event's custom fields. For example, set
Order_Number__cfrom the triggering record'sOrderNumberfield and setStatus__cto a text value such asShipped. - Save and activate the flow.
When the element runs, Salesforce publishes one event. The event object appears in the list of objects you can create, and it behaves like a custom object with a few differences:
- You can only create events. You cannot update or delete them, and you cannot look them up with a Get Records element in the usual way.
- The event has no record page, because it is not stored as a normal record.
- Fields you did not set stay empty, unless the field definition has a default value.
You can also publish several events in one element. Build a collection variable of event records, then use Use all values from a record collection in the Create Records element. This is the flow equivalent of passing a list to EventBus.publish, and it helps with limits.
Setting field values
The fields of an event are ordinary custom fields. Use a formula or a resource to fill them in. A few cases deserve attention:
- Text fields have a fixed length. If you assign a value longer than the field allows, the publish can fail. Truncate with a formula such as
LEFT({!$Record.Description}, 255). - Required fields must have a value. If a required field is empty, the publish fails.
- Keep the payload small. Send identifiers and the minimum data the subscriber needs, and let the subscriber query for more.
Publish behavior in a flow
Each platform event has a publish behavior setting. You choose it when you define the event. It decides when the event leaves your transaction:
| Publish behavior | When the event is published | Effect if the transaction rolls back |
|---|---|---|
| Publish After Commit | After the transaction commits successfully | The event is not published |
| Publish Immediately | As soon as the publish call runs | The event is still published |
This setting applies to flows in the same way as it applies to Apex, because the flow's Create Records element is a publish call. With Publish After Commit, a record-triggered flow that publishes an event and then fails on a later step does not send the event. With Publish Immediately, the event goes out even when the flow later fails and its changes roll back.
Choose the behavior from what the subscriber needs to see:
- Use Publish After Commit when the event says "this data changed". A subscriber that queries the record needs the change to be committed, or it sees old values.
- Use Publish Immediately when the event reports something that should be known whether or not the transaction succeeds, such as an error or an audit message.
Because the behavior belongs to the event definition, you cannot pick it per flow. If two flows need different behavior, define two events.
Where flows can publish
Different flow types publish in slightly different ways.
| Flow type | Typical use | Notes |
|---|---|---|
| Record-triggered (after save) | Announce that a record was created or changed | The most common choice. Publish after the save, so the data is final. |
| Record-triggered (before save) | Not suited | Before-save flows cannot create records, so they cannot publish events. |
| Screen flow | Publish when a user completes a form | The event is published when the flow reaches the Create Records element. |
| Autolaunched flow | Reusable publishing logic | Call it from Apex, from another flow, or from a process. |
| Scheduled-triggered flow | Publish a batch of events at a set time | Runs against a set of records, so watch the limits. |
Use an after-save record-triggered flow for most record-driven events. Before-save flows are designed for fast updates to the triggering record and do not allow creating other records.
Failure handling in a flow
A Create Records element can fail. A required field might be empty, or the event might not be available to the running user. Add a fault path to the element so you can handle the failure, instead of letting the whole flow error out.
A fault path starts from the element's fault connector. On this path you can:
- Write the error message to a log record, using the
$Flow.FaultMessageglobal variable. - Send an email to an administrator.
- Show a message on a screen, in a screen flow.
Be aware of what a failure means in each publish behavior. If the publish itself is rejected, for example because of a missing required field, the flow takes the fault path and you can react. If the event was accepted but the transaction later rolls back, a Publish After Commit event is never sent, and no fault path runs at the publish element, because that element had already completed.
Limits that matter in flows
Flows run in a transaction, so they share its limits. Publishing events adds two limits to keep in mind:
- Each event published counts against the allocation for events published per hour and per day. The amount depends on your edition and licenses.
- Events published from Apex or flows count toward the DML-style limit on publish calls in a transaction. Publishing in a loop is the usual cause of trouble.
The loop problem is as real in Flow Builder as in Apex. Do not place a Create Records element inside a Loop element, because each pass is a separate publish call. Instead, add each event to a collection inside the loop with an Assignment element. After the loop, publish the whole collection with one Create Records element.
This pattern matches the bulk approach you use in Apex, and it keeps the flow safe when a data load updates hundreds of records at once.
A worked example
Suppose an Order__c object has a Status__c picklist. You want to publish Order_Shipped__e when the status changes to Shipped.
- Create a record-triggered flow on
Order__c. Choose A record is created or updated, and run it After the record is saved. - Set the entry condition:
Status__cequalsShipped. Set When to Run the Flow for Updated Records to Only when a record is updated to meet the condition requirements. This stops the flow from publishing again each time an already-shipped order is edited. - Add a Create Records element for
Order_Shipped__e. SetOrder_Number__cto{!$Record.Name}andStatus__cto{!$Record.Status__c}. - Add a fault path that creates a log record from
{!$Flow.FaultMessage}. - Save and activate.
When a user changes an order to Shipped, the flow publishes one event. The Apex trigger from a later chapter receives it in Trigger.new and creates a follow-up task.
To check the result, open the Debug view in Flow Builder to confirm the element ran. Then confirm delivery from the subscriber side: look for the record the trigger created, or watch the event with a streaming monitor tool. The event itself is not queryable in the usual way.
Common mistakes
- Using a before-save flow. Before-save flows cannot create records, so the event cannot be published. Use an after-save flow.
- Publishing inside a loop. This hits limits in bulk operations. Collect events and publish once.
- Missing entry conditions. Without a condition that fires only on a real change, the flow publishes on every edit.
- Expecting a result from the subscriber. Publishing is fire-and-forget. If the flow needs an answer, a platform event is the wrong tool.
- Choosing the wrong publish behavior. An event that announces committed data needs Publish After Commit. A subscriber that queries too early sees old data.
Exercise
A record-triggered flow publishes Order_Shipped__e from a Create Records element inside a Loop element. The event uses Publish After Commit. Name two problems with this design and describe the fix for the first.
Show answer
First, publishing inside the loop makes one publish call per iteration, so a bulk update of many records can exceed limits. The fix is to add each event to a collection variable with an Assignment element inside the loop, then use a single Create Records element after the loop to publish the whole collection. Second, the flow has no fault path, so a failed publish, such as a missing required field, stops the whole flow with an unhandled error. Add a fault path to the Create Records element.
Exercise
An administrator wants a before-save record-triggered flow to publish an event when an Opportunity closes. Why does this not work, and what should they use?
Show answer
Before-save flows are meant for fast updates to the triggering record and cannot create other records. Publishing an event means creating a record of the event object, so it is not allowed there. They should use an after-save record-triggered flow with a Create Records element for the event.