Sub topic 3
Enabling CDC for standard and custom objects
This chapter assumes you know the Salesforce data model, can read basic Apex, and have read the earlier chapters on what CDC is and how the event bus works. Here you will turn on change events for the objects you care about and confirm that they work.
Nothing is published by default
Salesforce does not publish change events for any object until you select it. Once you select an object, Salesforce creates a change event type for it. Every create, update, delete, and undelete on that object then produces an event on the event bus.
Enable an object in Setup
- Open Setup and search for Change Data Capture.
- In Available Entities, pick the objects you want, such as Account or a custom object.
- Move them to Selected Entities and click Save.
The change takes effect as soon as you save. Only changes made after that point produce events. Salesforce does not backfill earlier changes.
Standard and custom objects
The steps are the same for both kinds, but the names differ.
| Object | Change event name | Channel |
|---|---|---|
| Account (standard) | `AccountChangeEvent` | `/data/AccountChangeEvent` |
| Invoice__c (custom) | `Invoice__ChangeEvent` | `/data/Invoice__ChangeEvent` |
For a custom object, the __c suffix becomes __ChangeEvent. The next chapter covers channel names in detail. You need the change event name now because Apex triggers and subscribers refer to it.
Not every standard object supports CDC. If an object is missing from Available Entities, it is not supported. Check the Change Data Capture Developer Guide for the current list.
Enable with metadata
Setup is fine for one org. To deploy the same configuration across orgs, use Metadata API. A PlatformEventChannelMember selects an entity on the built-in ChangeEvents channel.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>ChangeEvents</eventChannel>
<selectedEntity>AccountChangeEvent</selectedEntity>
</PlatformEventChannelMember>Save it as platformEventChannelMembers/ChangeEvents_AccountChangeEvent.platformEventChannelMember-meta.xml in your project. Then deploy it like any other metadata. Note that selectedEntity takes the change event name, not the object name.
Check that it works
Once an object is selected, you can write a trigger on its change event. The trigger fails to save if the object has not been enabled. For a quick runtime check, subscribe to the channel with a tool such as the empApi component. Then edit a record and watch the event arrive.
Common mistakes
- Expecting history. Events exist only for changes made after you enabled the entity.
- Using the object name in metadata. Metadata expects
AccountChangeEvent, notAccount. - Enabling everything "just in case". Each selected entity adds event volume and uses allocation.
- Forgetting sandboxes. Selections made in Setup do not carry between orgs. Deploy the metadata to promote them.
Exercise
You select the custom object Shipment__c in Change Data Capture Setup. What is its change event name, and does it include changes made yesterday?
Show answer
The change event name is Shipment_ChangeEvent, and the channel is /data/Shipment_ChangeEvent. It does not include yesterday's changes. Events are published only for changes made after the entity was selected.