Sub topic 1
What Change Data Capture is and when to use it
Prerequisites
This chapter assumes you already 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
insertandupdate). - How to read JSON.
- The general idea of publish/subscribe messaging, where senders publish messages and any number of receivers listen for them.
You do not need any prior experience with Salesforce streaming features. This chapter explains the idea and helps you decide whether to use it. Later chapters cover setup, event structure, and subscribers in detail.
The problem CDC solves
Many integrations need to know when a Salesforce record changes. A data warehouse needs fresh account data. An order system needs to know when a customer's shipping address changes. A support tool needs to react when a case is reassigned.
The usual first approach is polling. A scheduled job queries Salesforce every few minutes and asks which records have a SystemModstamp newer than the last run. Polling works, but it has costs:
- Most polls return nothing, yet each one uses an API request.
- Changes are seen only at the next poll, so latency is tied to the schedule.
- Shortening the interval lowers latency but raises API consumption.
- Deleted records no longer appear in a normal query, so you need extra calls to find them.
- You must store and manage the "last seen" timestamp yourself, and clock or transaction edge cases can cause missed records.
Change Data Capture (CDC) reverses the direction. Instead of asking Salesforce what changed, you subscribe, and Salesforce publishes a message each time a selected record changes.
What CDC is
Change Data Capture is a Salesforce streaming feature. When a record of a selected object is created, updated, deleted, or undeleted, Salesforce publishes a change event. A change event is a message that describes the change. Subscribers receive it in near real time.
Key properties:
- The platform generates the events. You do not write publishing code. You select the objects, and Salesforce does the rest.
- Events cover four change types. These are create, update, delete, and undelete. The
changeTypevalue in the event header tells you which one occurred. - Events are published after the transaction commits. A subscriber never sees a change that was rolled back.
- Update events carry only what changed. An update event contains the changed fields plus header metadata, not the whole record. A create event contains the populated field values.
- Events are delivered asynchronously. Your subscriber runs outside the transaction that made the change.
- Events are retained for a limited time. The event bus keeps them for 72 hours (three days), so a subscriber that was offline can catch up. Replay is covered in a later chapter.
Here is a simplified update event for an Account whose phone number changed. The 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 ChangeEventHeader says what happened, to which record, and who did it. The fields after it hold the new values. The name, industry, and every other unchanged field are absent. A subscriber that needs the full record must keep its own copy or query Salesforce.
How the flow works
The diagram shows the path from a record change to a subscriber.
Two points matter for design. First, publishing happens after the commit, so the original save is never slowed or blocked by subscribers. Second, subscribers are decoupled from each other. An Apex trigger, a Lightning web component, and an external system can all consume the same event without knowing about one another.
How CDC differs from related features
Salesforce has several streaming features, and they are easy to confuse. The comparison below helps you pick the right one.
| Feature | Who defines the message | What triggers it | Content | Typical use |
|---|---|---|---|---|
| Change Data Capture | Salesforce, based on the object | Any create, update, delete, or undelete on a selected object | Header plus changed fields | Keeping external systems in sync |
| Platform events | You, with a custom schema | Your code or a flow publishes them | Whatever fields you define | Business events and system-to-system messaging |
| PushTopic | You, with a SOQL query | Record changes that match the query | Fields named in the query | Older streaming integrations |
| Polling | Your client | A timer | Whatever your query returns | Simple or low-frequency needs |
CDC versus platform events
A platform event is a custom message that you design and publish deliberately. It represents a business occurrence, such as "Order Shipped," that may not map to any single record change. CDC events describe data changes that already happened, and Salesforce shapes them for you. If you want to say "this record changed," use CDC. If you want to say "this business thing happened," define a platform event.
CDC versus PushTopic
PushTopic is an older streaming mechanism that uses a SOQL query to decide which changes to send. It is query-based, so it has a limited set of supported queries and a different event format. CDC is not query-based. It covers all changes to an object, includes delete and undelete events, and carries a richer header with details such as the change origin and transaction key. For new work, CDC is generally the better fit for data synchronization.
CDC versus polling
Polling is still reasonable when you need data only once a day, when the object is not supported by CDC, or when you cannot run a long-lived subscriber. For anything near real time, CDC avoids the wasted calls and the schedule-bound delay.
When to use CDC
CDC fits well when:
- An external system must mirror Salesforce data, such as a data warehouse, search index, or ERP.
- You need to know about deletes and undeletes as well as creates and updates.
- You want to react to changes without adding load to the original transaction.
- Several consumers need the same stream of changes.
- You want a changes-only payload to reduce the amount of data sent.
CDC is a poor fit when:
- You need to validate or reject a change before it commits. Use a before trigger, a validation rule, or a flow instead.
- You need strict, synchronous confirmation that the downstream system received the change. CDC is asynchronous and at-least-once style consumption needs your own tracking.
- The change must be processed in the same transaction as the original save.
- You are signaling something that is not a record change. Use a platform event.
- The object is not available for CDC. Support varies by object, so check the list in Setup.
A short decision process
Use these questions in order when you evaluate a requirement:
- Must the logic run inside the original transaction or be able to block it? If yes, use a regular trigger or flow, not CDC.
- Is the trigger for the work a change to a record? If no, consider a platform event.
- Is the consumer able to run continuously, or at least reconnect within 72 hours? If no, consider scheduled queries or an ETL tool.
- Do you need only changed fields, plus deletes and undeletes? If yes, CDC is a strong candidate.
- Can you accept eventual consistency and add a reconciliation step? If yes, proceed with CDC.
What you will build in this tutorial
The following chapters follow one thread from setup to design. You will enable CDC for objects and learn the channel names. You will read the structure of change events and the behavior of each change type. You will react to events with an Apex trigger, subscribe from a Lightning web component and from external clients, and resume from a replay ID after a disconnect. You will handle gap and overflow events, scope events with custom channels, and check security and limits. You will end by testing your trigger and reviewing integration patterns and common pitfalls.
Exercise
A team wants a nightly job to export account changes and also needs to know when accounts are deleted. Another team wants to block users from saving an Account unless a related record exists. Which team should consider CDC, and why does the other team need something different?
Show answer
The first team can use CDC because it publishes delete events along with creates, updates, and undeletes, and it delivers changes without polling. The second team needs a before trigger, validation rule, or flow. CDC events are published after the commit, so they cannot block or reject the save.
Exercise
Your integration reacts to a "Customer Onboarding Complete" milestone that is not tied to a single field change on one record. Should you model it with CDC or a platform event, and why?
Show answer
Use a platform event. It is a custom message you define and publish when the business milestone occurs. CDC only describes record changes that Salesforce detects on selected objects.