when yo use change data capture and platform events in salesfroce ?
Change Data Capture vs Platform Events
Both are part of Salesforce's event-driven architecture. They use a publish/subscribe model on the Event Bus, so publishers and subscribers are decoupled.
Change Data Capture (CDC)
Use it when you need to react to changes in Salesforce records.
- Salesforce publishes the event automatically when a record is created, updated, deleted, or undeleted. You don't write any publishing code.
- The event carries the changed fields, plus header details like the change type and the user who made the change.
- You enable it per object (Setup > Change Data Capture), and the channel name looks like
/data/AccountChangeEvent.
Typical use cases:
- Syncing Salesforce data to an external system such as an ERP, data warehouse, or SAP, in near real time.
- Keeping a replica or cache up to date without polling.
- Audit or history tracking outside Salesforce.
- Triggering Apex when specific fields on a record change.
Platform Events
Use them when you need to send custom business messages, not tied to a record change.
- You define the event schema (a custom object ending in
__e) with your own fields. - You publish explicitly, through Apex, Flow, the API, or an external system.
- Subscribers can be Apex triggers, Flows, LWC/Aura (via empApi), or external systems (via CometD or the Pub/Sub API).
Typical use cases:
- Integration with external systems, for example telling a billing system that "Order approved, start invoicing".
- Decoupling processes, for example CPQ quote approved → trigger order creation and fulfillment.
- Inbound events from external systems (IoT, payment gateway) that need to trigger logic in Salesforce.
- Fire-and-forget processing, or breaking out of transaction and governor limit constraints.
- Notifying the UI in real time.
Key differences
| CDC | Platform Events | |
|---|---|---|
| Trigger | Automatic on record DML | Explicit publish |
| Schema | Fixed, mirrors the object's fields | Custom, you define it |
| Payload | Changed fields only | Whatever you define |
| Purpose | Data replication and sync | Business or process messaging |
| Setup | Enable per object | Create the event definition |
How to say it in an interview
"I use CDC when the source of truth is a Salesforce record and an external system needs to stay in sync with its changes. Salesforce publishes the event automatically, so there's no custom code on the publisher side. I use Platform Events when I need to communicate a business event or custom payload that isn't just a record change, or when I need loose coupling between processes or systems. For example, in an Order-to-Cash flow, CDC on Account keeps the ERP customer master in sync, while a Platform Event like Order_Approved__e tells the billing system to generate an invoice."
Follow-up points they may ask
- Replay: Both retain events for 72 hours. Subscribers can replay from a stored replay ID to recover missed events.
- Limits: Event delivery is asynchronous, and there are daily event delivery and publishing allocations that depend on your edition.
- Publish behavior: Platform Events can be "Publish Immediately" or "Publish After Commit". Use After Commit when the subscriber depends on the data being saved.
- Transaction boundary: Event publishing isn't rolled back with DML if you use Publish Immediately.
- CDC gotcha: Enabling CDC on an object doesn't include everything by default. Check which standard objects support it, and note that Enterprise Edition allows only a limited number of selected entities unless you have an add-on license.
Comments
Post a Comment