Sub topic 4
Channel names for change events
This chapter assumes you can read basic Apex and JavaScript. It also assumes you know that CDC publishes events to an event bus and that you enable CDC per object in Setup. Here you learn how to work out the exact channel name a subscriber needs.
Why the name matters
A subscriber listens on a channel. If the name is wrong, the subscription fails or you receive nothing. Channel names are case-sensitive and follow fixed rules, so you can derive them instead of looking them up.
The naming rules
Every change event channel begins with the /data/ prefix. The rest depends on what you want to receive.
| What you want | Channel name | Example |
|---|---|---|
| Changes to one standard object | `/data/<ObjectName>ChangeEvent` | `/data/AccountChangeEvent` |
| Changes to one custom object | `/data/<CustomObject>__ChangeEvent` | `/data/Invoice__ChangeEvent` |
| Changes to a custom object in a managed package | `/data/<namespace>__<Object>__ChangeEvent` | `/data/acme__Invoice__ChangeEvent` |
| Changes to every object you selected | `/data/ChangeEvents` | `/data/ChangeEvents` |
| A custom channel you defined | `/data/<ChannelName>__chn` | `/data/SalesEvents__chn` |
For a custom object, replace the trailing __c with __ChangeEvent. The object Invoice__c becomes Invoice__ChangeEvent.
One name, three uses
The entity name without /data/ is the name of the change event object. You use it in different places:
- Apex triggers use the event object name:
trigger AccountChangeTrigger on AccountChangeEvent (after insert). - empApi, CometD, and Pub/Sub API clients use the full channel name, including
/data/. - SOQL cannot query change events, so the name is only for subscribing.
The same rules apply to Contact (ContactChangeEvent), Opportunity (OpportunityChangeEvent), and any other standard object that supports CDC.
Using a channel in a subscriber
This snippet shows the full channel name passed to empApi. The subscription is the same as in the Lightning web component from the Topic Spine. Only the channel string changes.
import { subscribe } from 'lightning/empApi';
const ACCOUNT_CHANNEL = '/data/AccountChangeEvent';
const INVOICE_CHANNEL = '/data/Invoice__ChangeEvent';
const ALL_SELECTED_CHANNEL = '/data/ChangeEvents';
export function listen(channel, handler) {
// -1 means "deliver only new events"
return subscribe(channel, -1, handler);
}
listen(ACCOUNT_CHANNEL, (message) => {
console.log(message.data.payload.ChangeEventHeader.entityName);
});The all-changes channel
/data/ChangeEvents delivers events for every object you have enabled. Each event still carries ChangeEventHeader.entityName, so one handler can branch on it. This is convenient for a small number of objects. Subscribing to it can also use up your delivery allocation faster, because you receive every enabled object's events whether you need them or not. A later chapter covers custom channels, which let you pick a subset.
Common errors
- Forgetting
/data/.AccountChangeEventworks in a trigger but not as a subscription channel. - Using
__cin the channel./data/Invoice__cdoes not exist. Use__ChangeEvent. - Subscribing before enabling the object. The channel name is valid only after the object is selected in Setup. Until then, the subscription fails.
- Wrong case.
/data/accountchangeeventdoes not match.
Exercise
What are the channel name and the Apex trigger target for changes to a custom object called Shipment__c?
Show answer
The channel name is /data/Shipment__ChangeEvent. The trigger is declared on Shipment__ChangeEvent (after insert). The channel adds the /data/ prefix, and the trigger uses only the event object name.
Exercise
A subscriber on /data/Account receives nothing, although Account is enabled for CDC. What is the likely cause?
Show answer
The channel name is wrong. It should be /data/AccountChangeEvent. For a standard object, ChangeEvent must be appended to the object name.