Sub topic 6
Fire-and-forget: publishing events without waiting
This chapter assumes you know Apex basics and triggers, the Salesforce data model, and the difference between synchronous and asynchronous calls from earlier chapters. It also assumes you have read the request-reply chapter, because fire-and-forget is easiest to understand by contrast.
What fire-and-forget means
In fire-and-forget, the sender hands a message to a channel and moves on. It does not wait for the receiver to process the message, and it does not get a business answer back. The only thing the sender learns is whether the channel accepted the message.
This changes three properties compared with request-reply:
- Timing: the publisher finishes as soon as the event is accepted. Subscribers run later.
- Coupling: the publisher does not know who subscribes. You can add or remove subscribers without touching the publisher.
- Failure behavior: a slow or broken subscriber does not fail the publisher's transaction. The cost is that the publisher cannot see a subscriber's error directly.
Platform events
In Salesforce, the main tool for this pattern is the platform event. A platform event is a custom message type, defined like an object but with the __e suffix. Publishers write events to the event bus. Subscribers read from the bus. Subscribers can be Apex triggers, Flows, or external systems using the Pub/Sub API or CometD.
Events are not stored like normal records. They stay on the bus for a limited time (72 hours for standard-volume events), and subscribers can replay events within that window.
Publishing an event from Apex
The example below assumes an event named Order_Shipped__e exists in the org, with the text fields Order_Number__c and Carrier__c.
Order_Shipped__e evt = new Order_Shipped__e(
Order_Number__c = '00123',
Carrier__c = 'ACME'
);
Database.SaveResult result = EventBus.publish(evt);
if (!result.isSuccess()) {
for (Database.Error err : result.getErrors()) {
System.debug(LoggingLevel.ERROR, 'Publish failed: ' + err.getMessage());
}
}EventBus.publish returns a Database.SaveResult. A successful result means Salesforce accepted the event. It does not mean any subscriber has processed it, or that one exists. Check isSuccess() and log failures, as shown above.
Subscribing with an Apex trigger
A subscriber is an after insert trigger on the event. The trigger runs in its own transaction, separate from the publisher.
trigger OrderShippedSubscriber on Order_Shipped__e (after insert) {
for (Order_Shipped__e evt : Trigger.new) {
System.debug('Shipped: ' + evt.Order_Number__c + ' via ' + evt.Carrier__c);
}
}In real code, you would update records or call another system here. Each event carries a ReplayId, a number that increases over time. Subscribers use it to resume from a known position.
Choosing when events are published
Platform events have a publish behavior setting, and it matters when you publish inside a transaction:
| Setting | When the event is published | Typical use |
|---|---|---|
| Publish After Commit | Only after the publishing transaction commits | Notify others about data that was saved |
| Publish Immediately | As soon as `publish` is called, even if the transaction later rolls back | Audit or logging events that must survive a rollback |
Use Publish After Commit for most business events. Otherwise a subscriber could react to a change that never got saved.
Change Data Capture
Change Data Capture (CDC) is a related feature. Instead of events you define and publish, Salesforce publishes a change event automatically when a record is created, updated, deleted, or undeleted. Use CDC when an external system needs to mirror record changes. Use custom platform events when you want to announce a business fact, such as "order shipped", that is not a plain record change.
Common errors
- Assuming a successful publish means success downstream. It only means the bus accepted the event. Monitor subscribers separately.
- Expecting a response. If the sender needs an answer to continue, use request-reply instead.
- Publishing inside loops. Publish a list of events in one
EventBus.publish(List<SObject>)call to save on limits. - Relying on event order across publishers. Events from one publisher are ordered, but do not assume a global order.
- Forgetting that a failing trigger subscriber retries. An unhandled exception can cause the platform to retry the batch, so repeated runs must be safe.
Exercise
An Apex method publishes an OrderShipped_e event with Publish After Commit, and then the surrounding transaction rolls back because of a validation error. Does the subscriber receive the event? What would change if the event used Publish Immediately?
Show answer
With Publish After Commit, the event is never published, so the subscriber receives nothing. That is the desired behavior, because the order change was not saved. With Publish Immediately, the event is published when publish is called, so subscribers receive it even though the transaction rolled back. They might then act on a change that does not exist.
Exercise
A publisher checks result.isSuccess() and it returns true. A teammate says this proves the warehouse system processed the shipment. What is wrong with that claim?
Show answer
A successful result only means the event bus accepted the event. Processing happens later and separately in each subscriber, and it can fail or be delayed. To confirm processing, you need subscriber-side logging, monitoring, or an explicit acknowledgment event.