Sub topic 1
What Change Data Capture is and when to use it
Prerequisites
This chapter assumes you know the following:
- The Salesforce data model: standard and custom objects, records, and fields.
- Basic Apex, including triggers and DML (data manipulation language statements such as insert and update).
- How to read JSON.
- The general idea of publish/subscribe messaging, where a sender publishes messages and any number of receivers subscribe to them.
The working model
Change Data Capture (CDC) publishes a change event whenever a selected Salesforce record is created, updated, deleted, or undeleted. A change event is a message that describes what happened to one record, or to a group of records changed in the same operation. Salesforce sends these messages to the event bus. Subscribers read them from there. A subscriber can be an Apex trigger, a Lightning web component, or an external system.
Three properties shape how you design with CDC:
- Asynchronous. The event is published after the transaction commits. Your subscriber runs later, outside the user's save operation.
- Field-level. For an update, the event carries the changed fields, not the whole record. It also carries a header with metadata such as the change type and record IDs.
- Decoupled. The code that saves the record does not know who is listening. You can add or remove subscribers without editing the original code.
Here is a simplified update event for an Account. The ID and timestamp values are placeholders.
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "UPDATE",
"changedFields": ["Phone", "LastModifiedDate"],
"changeOrigin": "com/salesforce/api/rest/60.0",
"transactionKey": "example-transaction-key",
"sequenceNumber": 1,
"commitTimestamp": 1700000000000,
"commitUser": "005XXXXXXXXXXXXXXX",
"commitNumber": 1700000000000000
},
"Phone": "555-0100",
"LastModifiedDate": "2026-12-01T09:00:00.000Z"
}The header says an UPDATE touched Phone on one Account. Only the changed values follow it. Later chapters cover each header field in detail.
When to use CDC
CDC fits when other systems or automation must stay in step with Salesforce data. Common cases include:
- Keeping an external database, data warehouse, or search index in sync.
- Starting follow-up work when a specific field changes, such as creating a task when a phone number is updated.
- Refreshing a user interface when a record changes elsewhere.
How CDC differs from the alternatives
| Approach | How it works | Main drawback |
|---|---|---|
| Polling | A client queries Salesforce on a schedule. | Wastes API calls. Changes can be missed or seen late. |
| PushTopic | A SOQL query defines which changes stream out. | Limited to the fields and conditions in the query. It does not capture the full set of change types in the same way as CDC. |
| Platform events | You define a custom event and publish it from your own code. | You must write the publishing code and decide the payload. |
| CDC | Salesforce publishes events automatically for selected objects. | You get changes to the objects you enable. You do not control the payload shape. |
Choose CDC when you want a record-level change feed without writing publishing code. Choose platform events when you need to announce a business event that is not a simple record change.
Exercise
A team wants to stop users from saving an Opportunity unless a discount field is under 30 percent. Is CDC the right tool? Why or why not?
Show answer
No. Change events are published after the transaction commits, so they cannot reject the save. A before-save Apex trigger or a validation rule is the right tool. CDC suits reacting to the change afterward, such as syncing it to another system.
Exercise
Name two reasons to prefer CDC over polling the REST API every minute.
Show answer
CDC pushes changes as they happen, so latency is lower and no changes are missed between polls. It also avoids spending API calls on queries that often return nothing new.