Sub topic 3
Enabling CDC for standard and custom objects
This chapter shows how to turn on Change Data Capture (CDC) for the objects you care about. It covers the Setup page, the Metadata API route, how to confirm that events are flowing, and the mistakes that cause silent failures. It assumes you know the Salesforce data model (standard and custom objects, fields), can read basic Apex, and have met the idea of an event bus from the previous chapter.
What "enabling" means
CDC does not publish changes for every object by default. Salesforce publishes change events only for objects that you select. Selecting an object does three things:
- It makes Salesforce start publishing a change event to the event bus after each create, update, delete, or undelete on that object.
- It makes the object's change event channel available to subscribers, for example
/data/AccountChangeEvent. - It makes the matching change event type, such as
AccountChangeEvent, usable in an Apex trigger.
Enabling is not retroactive. Changes made before you enabled the object are never published, and CDC will not backfill them. If a downstream system needs the current state, load it separately (for example with a bulk query) and then rely on CDC for later changes.
Enabling in Setup
The Setup page is the quickest way to enable an object, and it is the best choice for a developer org or a first experiment.
- In Setup, enter
Change Data Capturein Quick Find and open the page under Integrations. - In the Available Entities list, find the object. Standard objects appear by label, such as Account. Custom objects appear by label with their
__cname. - Move the object to the Selected Entities list.
- Click Save.
Events start flowing shortly after the save completes. To stop, move the object back to Available Entities and save. Disabling stops new events from being published. Events already on the bus stay available until their retention window ends, a topic covered in the replay chapter.
Which objects can you select
Not every object supports CDC. The Available Entities list shows only the objects that do. Some points to keep in mind:
- Many commonly used standard objects are supported, including Account, Contact, Lead, Opportunity, Case, and User. The Change Data Capture Developer Guide lists the full set.
- All custom objects are supported.
- If a standard object is missing from the list, it is not supported, or it belongs to a feature your org does not have.
- Some standard objects have a companion change event for related data, but the support list is the authority. Check it before you design around an object.
Allocation of selected entities
Each org has a cap on how many entities you can select. In common editions such as Developer and Enterprise, the default is a small number, five at the time of writing. An add-on license raises the cap. Check the current figure for your edition in the Developer Guide, because it changes over time.
The cap matters in design. A sandbox where you tried five objects may leave you with no room for a sixth. Review what is selected before you add more, and remove experiments you no longer need.
Enabling with the Metadata API
For anything beyond a first experiment, enable CDC through metadata so the configuration lives in source control and deploys the same way to every org. Selection is stored in a PlatformEventChannelMember record that ties an entity to a channel. For the default change event channel, the channel is named ChangeEvents.
This member enables Account on the default channel. The file path is force-app/main/default/platformEventChannelMembers/ChangeEvents_AccountChangeEvent.platformEventChannelMember-meta.xml.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>ChangeEvents</eventChannel>
<selectedEntity>AccountChangeEvent</selectedEntity>
</PlatformEventChannelMember>Note that selectedEntity takes the change event name (AccountChangeEvent), not the object name (Account). The file name follows the pattern <channel>_<ChangeEventName>.
Deploy it with the Salesforce CLI:
sf project deploy start \
--source-dir force-app/main/default/platformEventChannelMembers \
--target-org my-sandboxIf you deploy with a package.xml manifest instead, list the member under the PlatformEventChannelMember type:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>ChangeEvents_AccountChangeEvent</members>
<name>PlatformEventChannelMember</name>
</types>
<version>60.0</version>
</Package>Custom objects
The pattern is the same, but the change event name follows the object API name. For a custom object Invoice__c, the change event is Invoice__ChangeEvent. Drop the c from the suffix and replace it with ChangeEvent.
| Object API name | Change event name | Member file base name |
|---|---|---|
| `Account` | `AccountChangeEvent` | `ChangeEvents_AccountChangeEvent` |
| `Invoice__c` | `Invoice__ChangeEvent` | `ChangeEvents_Invoice__ChangeEvent` |
| `Order__c` in a namespace `acme` | `acme__Order__ChangeEvent` | `ChangeEvents_acme__Order__ChangeEvent` |
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>ChangeEvents</eventChannel>
<selectedEntity>Invoice__ChangeEvent</selectedEntity>
</PlatformEventChannelMember>Confirming that CDC is on
Do not assume. Check two things after enabling.
First, list what is selected. The Tooling API exposes the members:
sf data query --use-tooling-api \
--query "SELECT DeveloperName, EventChannel, SelectedEntity FROM PlatformEventChannelMember" \
--target-org my-sandboxYou should see one row for each enabled entity. If the row you expect is missing, the deploy did not apply or you saved the wrong list in Setup.
Second, trigger a real change and watch for the event. The easiest check is the empApi sample from the subscriber chapter, or a trigger that logs to the debug log. Update a record on the enabled object, then confirm that an event arrives with the expected changeType. The update should arrive as one event whose header lists the fields that changed.
What changes after you enable an object
Enabling affects more than subscribers. Keep these effects in mind.
- Every transaction that changes the object now also publishes an event. That is extra work on the save path, and it counts against event delivery and publishing allocations, covered in the limits chapter.
- An Apex trigger on the change event type can now run. Its logic runs asynchronously, after the original transaction commits, in its own context and as a special automated process user. It is not part of the user's save.
- Events carry data. Treat the channel as you would a data export, because anything that can subscribe sees field values. The security chapter covers who gets what.
Common mistakes
- Using the object name where the change event name is required. Metadata needs
AccountChangeEvent, and channels need/data/AccountChangeEvent.Accountfails in both places. - Expecting history. A subscriber that connects after you enable CDC sees only new changes, plus whatever retained events its replay ID selects. Nothing from before enabling exists.
- Enabling in production but not in the sandbox, or the reverse. Selection is org configuration. Tests that rely on a populated channel can behave differently across orgs unless the member is deployed everywhere.
- Exceeding the entity cap. A deploy or a save fails when you go over the limit. Remove unused entities first.
- Forgetting that field-level behavior is separate. Selecting an object publishes all of its supported fields. You cannot choose individual fields at this step.
Summary of the steps
- Check that the object is supported and that you have a free slot under the entity cap.
- Select it in Setup under Change Data Capture, or deploy a
PlatformEventChannelMemberon theChangeEventschannel. - Query
PlatformEventChannelMemberto confirm the selection. - Change a record and check that an event arrives.
The next chapter covers channel names, including the per-object channels you subscribed to in the checks above.
Exercise
You need CDC for the custom object Shipment__c, and you want the setting stored in source control. What are the channel and selectedEntity values for the PlatformEventChannelMember, what is the member file base name, and what will existing records look like to a subscriber after you deploy?
Show answer
The channel is ChangeEvents, the default change event channel. selectedEntity is Shipment__ChangeEvent, and the file base name is ChangeEvents_Shipment__ChangeEvent. Existing records produce no events, because CDC does not backfill. A subscriber sees only changes made after enabling, so any needed starting data must be loaded separately.
Exercise
A teammate saves Account in the Setup page but a subscriber on /data/ContactChangeEvent receives nothing after a contact is edited. What is the most likely cause and how do you confirm it?
Show answer
Contact was never selected. Selection is per entity, so enabling Account publishes only Account changes. Confirm by running the Tooling API query on PlatformEventChannelMember and checking that no row has SelectedEntity equal to ContactChangeEvent. Then select Contact, or deploy the member, and edit a contact again.