Sub topic 15
When to Use Platform Events and When to Use Something Else
This chapter assumes you have read the earlier chapters of this tutorial. You should know how to define, publish and subscribe to a platform event, and the basics of Apex transactions and governor limits. It does not add new syntax. It helps you decide whether a platform event is the right tool for a given requirement.
What Platform Events Are Good At
A platform event is a custom message that you define, publish to the event bus, and deliver to any number of subscribers. Four properties make it useful:
- Decoupling. The publisher does not know who subscribes. You can add a flow, a trigger, a Lightning Web Component or an external system later without changing the publisher.
- A separate transaction for subscribers. An event trigger runs in its own transaction, after the publisher has finished. Its governor limits are separate from the publisher's, and a failure in the subscriber does not roll back the publisher's work.
- Many kinds of subscribers. Apex triggers, event-triggered flows,
empApicomponents and Pub/Sub API clients can all receive the same event. - Replay. Subscribers can resume from a replay ID within the retention window, so a short outage does not have to lose messages.
Platform events are a weaker fit when you need an immediate, synchronous answer, a guaranteed rollback, or an automatic feed of record changes.
The Alternatives
Change Data Capture
Change Data Capture (CDC) publishes a change event whenever a record is created, updated, deleted or undeleted. You do not write publishing code. You enable the object, and Salesforce publishes the events for you.
Choose CDC when the thing you want to announce is "this record changed". This is the usual case for syncing data to an external system. Choose a platform event when the message is a business fact you define yourself, such as "Order shipped", that may not match any single record change. A platform event also lets you shape the payload, so you can send only the fields a subscriber needs.
PushTopic and Generic Streaming
PushTopic events notify subscribers when records that match a SOQL query change. Generic streaming sends custom text payloads over a channel you create. Both belong to the older Streaming API. For new work, platform events and CDC cover the same needs and are the better choice. You will mostly meet PushTopic and generic streaming in existing code. Replacing them is optional unless you hit one of their limitations.
Outbound Messages
An outbound message is a SOAP call that a workflow action sends to an external endpoint. It is a point-to-point integration with one endpoint. It does not fan out to several subscribers, and it assumes the receiver speaks SOAP. If you need several consumers, or a modern API such as Pub/Sub API, use a platform event or CDC.
Direct Apex: Triggers, Queueable and Batch
If one piece of code in the same org needs to do extra work after a save, you may not need a message at all. A trigger handler or a Queueable job is simpler. It has fewer moving parts and is easier to debug, because you can follow one call chain.
public class FollowUpJob implements Queueable {
private final List<Id> orderIds;
public FollowUpJob(List<Id> orderIds) {
this.orderIds = orderIds;
}
public void execute(QueueableContext context) {
List<Task> tasks = new List<Task>();
for (Id orderId : orderIds) {
tasks.add(new Task(
Subject = 'Follow up on order',
Status = 'Not Started',
WhatId = orderId
));
}
insert tasks;
}
}A Queueable job runs asynchronously in its own transaction, as an event trigger does. It is a good fit when there is exactly one consumer and it lives in your own code. It is a poor fit when you want unrelated teams or systems to subscribe without changing your code. Batch Apex suits work over large record sets that you can query. It is not a messaging tool.
Flow Without Events
If the logic is "when a record changes, do something to related records", a record-triggered flow is often enough. Add a platform event only when the work should run in a separate transaction, or when other subscribers need to hear about it.
Comparison Table
| Option | Who defines the payload | Publisher code needed | Subscribers | Best for |
|---|---|---|---|---|
| Platform event | You | Yes (Apex, flow or API) | Many, of several types | Custom business messages, decoupled integration |
| Change Data Capture | Salesforce, from the object | No | Many, same types as events | Streaming record changes to other systems |
| PushTopic | A SOQL query | No | Streaming API clients | Existing legacy streaming code |
| Generic streaming | You (text) | Yes | Streaming API clients | Existing legacy streaming code |
| Outbound message | Workflow field selection | No | One SOAP endpoint | Simple SOAP integrations that already exist |
| Queueable or Batch | Your own code | Yes | Your own class | Single-consumer async work in one org |
A Decision Path
The following flow reduces the choice to a few questions.
Trade-offs to Weigh Before Choosing Events
Asynchronous Delivery
Publishing returns once Salesforce accepts the event. It does not wait for any subscriber. The publisher cannot learn whether a subscriber succeeded, unless the subscriber publishes a reply event or writes a record. If your requirement says "the user must see an error when step two fails", a platform event is the wrong tool.
Transaction Behavior
Events published after commit are only sent if the publishing transaction succeeds. Events published immediately are sent even if the transaction later rolls back. That choice is part of the design. Pick the behavior that matches the meaning of the message. For example, "Order shipped" should usually be published after commit, so that no subscriber hears about an order that was rolled back.
Debugging Cost
A direct call is one stack trace. An event design splits the work across a publisher transaction and one or more subscriber transactions. Subscriber failures show up in separate logs, and you have to correlate them with something such as an order number in the payload. Budget time for logging and monitoring, and for tests in both directions.
Ordering and Duplicates
Events are delivered in replay ID order within a channel, but subscribers should still be written defensively. A retry after EventBus.RetryableException can process an event more than once. Make subscriber logic idempotent. For example, use an external ID and upsert instead of a plain insert when a duplicate would be harmful.
Allocations and Limits
Every published event counts against your org's publishing allocation, and each subscription type has its own delivery limits. High event volume with many subscribers can reach those allocations. Check the current numbers for your edition in the Salesforce documentation and monitor usage, as covered in the previous chapter. Do not rely on a figure remembered from an older release.
Subscriber Identity
Apex event triggers run as the Automated Process user by default. If your logic depends on the identity or sharing of the original user, carry the needed data in the payload and decide explicitly how to handle ownership and permissions.
Common Mistakes
- Using an event as a function call. If the publisher needs a return value, use a method call or an API request.
- Using an event for a single consumer in the same org. A
Queueablejob is simpler and easier to trace. - Rebuilding CDC by hand. If every payload is just "record X changed", enable CDC instead of writing publishing code in every trigger.
- Putting everything in the payload. Send identifiers and the facts a subscriber needs. A subscriber can query for the rest, and the payload stays small and stable.
- Ignoring duplicates. Retries and replays mean a subscriber can see the same event twice.
Worked Decision
Suppose the requirement is: "When an order is marked shipped, create a follow-up task for the account team. A warehouse system outside Salesforce also needs to know."
- The task creation is in-org work. It could be a record-triggered flow or a
Queueablejob on its own. - The warehouse system is an external subscriber. Order shipping is a business fact, so a platform event named
Order_Shipped__e, published after commit, fits. The external system subscribes through Pub/Sub API. - Because you already have the event, the follow-up task can subscribe to the same event with a trigger or flow. The publisher stays unchanged when you add either consumer.
If the warehouse only needed to mirror the Order record's fields, CDC would remove the need to write any publishing code.
Exercise
A team wants to keep an external data warehouse in sync with every field change on the Account object. They are considering a custom platform event that an Account trigger publishes. What is the simpler alternative, and when would the custom event still be the better choice?
Show answer
Change Data Capture is simpler. After you enable it for Account, Salesforce publishes a change event for each create, update, delete and undelete, and no publishing code is needed. The custom platform event is better when the message is a business fact rather than a raw record change (for example, "Account tier upgraded"), or when you want to control and limit the payload to specific fields.
Exercise
A trigger needs to run extra logic after an Opportunity is closed. Only one Apex class in the same org will ever use this logic, and the user must see an error on the page if it fails. Should you use a platform event?
Show answer
No. The user must see the error, and a platform event is asynchronous, so the publisher cannot find out that a subscriber failed. With one consumer in the same org, call the logic synchronously from the trigger handler. If it can run later and the user does not need the result, a Queueable job is the simpler async choice.