Sub topic 3
Choosing publish after commit or publish immediately
This chapter assumes you can read Apex, know that a transaction either commits or rolls back, and have already defined a platform event such as Order_Shipped__e. It covers one setting on the event definition, Publish Behavior, and how it changes what subscribers see.
The two options
Every platform event definition has one of two publish behaviors:
- Publish After Commit: the event is held until the publishing transaction commits. If the transaction rolls back or fails, Salesforce never publishes the event.
- Publish Immediately: the event is published as soon as your code calls
EventBus.publish. A later rollback does not recall it.
The setting applies to every publisher of that event, whether it is Apex, a flow, or the REST API. You can't pick a behavior per call.
What it looks like in code
The publishing code is the same for both behaviors. Only the event definition differs.
Savepoint sp = Database.setSavepoint();
Account acct = new Account(Name = 'Acme');
insert acct;
Database.SaveResult sr = EventBus.publish(
new Order_Shipped__e(Order_Number__c = '00012345', Status__c = 'Shipped')
);
// Something goes wrong, so the work is undone.
Database.rollback(sp);Here is what subscribers see after the rollback:
| Behavior | Account saved | Event delivered |
|---|---|---|
| Publish After Commit | No | No |
| Publish Immediately | No | Yes |
Choosing between them
Use Publish After Commit when the event announces something that really happened in Salesforce data. Examples are "order shipped" or "case escalated". Two things follow from this choice:
- Subscribers never hear about changes that were rolled back.
- When a subscriber trigger runs, the publisher's records are already committed, so queries in the subscriber can find them.
Use Publish Immediately when the event must survive a failure, or when you don't depend on the transaction's data. Common cases are:
- Logging errors from a transaction that is about to fail.
- Sending progress updates from a long-running process.
- Publishing from a context where the transaction might not commit for a while.
The risk is a mismatch. A subscriber may act on an event for data that was never saved, or it may run before the publisher's transaction finishes. A subscriber that looks up a record by ID can then find nothing.
Common mistakes
- Using Publish Immediately by default. It looks simpler, but it produces events for rolled-back work. Start with Publish After Commit and change it only for a reason you can state.
- Expecting a rollback to recall an event. Once a Publish Immediately event is on the bus, it can't be undone.
- Changing the behavior late. The setting changes what every existing publisher does. Review all publishers and subscribers before you change it on an event that is in use.
Exercise
A trigger on Case publishes an Error_Logged__e event inside a catch block, then rethrows the exception so the whole transaction rolls back. Which publish behavior lets subscribers still receive the error event, and why?
Show answer
Publish Immediately. The event is sent when EventBus.publish runs, so the rollback caused by the rethrown exception does not discard it. With Publish After Commit the event would be dropped, because the transaction never commits.