Sub topic 4
Channel names for change events
This chapter assumes you know the Salesforce data model, can read basic Apex, and understand publish/subscribe at a high level. The previous chapters covered the event bus and how to enable Change Data Capture (CDC) for an object. Here you learn how to name the channel that carries those events.
Why the channel name matters
A channel is the named stream a subscriber listens to. A subscriber that uses the wrong name fails in one of two ways. It receives an error, or it subscribes successfully and never receives any events. The second case is harder to debug because nothing looks broken.
Each object you enable in Setup gets its own change event channel. The channel name is built from the object's API name by fixed rules. You never choose it.
The default channel for one object
Every selected object has a default channel. The name has two parts: the /data/ prefix and the change event name.
For a standard object, append ChangeEvent to the API name.
Accountbecomes/data/AccountChangeEvent.Contactbecomes/data/ContactChangeEvent.Opportunitybecomes/data/OpportunityChangeEvent.
For a custom object, drop the trailing __c and append __ChangeEvent.
Invoice__cbecomes/data/Invoice__ChangeEvent.
For a custom object from a managed package, keep the namespace prefix.
acme__Invoice__cbecomes/data/acme__Invoice__ChangeEvent.
The prefix is /data/. Platform events use /event/, and the older PushTopic API uses /topic/. Mixing these up is the most common naming error.
Two names for the same thing
The same change event has two spellings, depending on who is asking.
- Apex triggers use the event's entity name, such as
AccountChangeEvent. There is no prefix, because the trigger is declared on an object type. - Subscribers outside Apex use the channel name, such as
/data/AccountChangeEvent.
The trigger from the Topic Spine example shows the entity form:
trigger AccountChangeTrigger on AccountChangeEvent (after insert) {
// handle events
}The Lightning web component example uses the channel form:
channelName = '/data/AccountChangeEvent';The same applies to custom objects. The trigger is trigger InvoiceChangeTrigger on Invoice__ChangeEvent (after insert). The subscription channel is /data/Invoice__ChangeEvent.
The all-objects channel
Subscribing to one channel per object gets tedious when you track many objects. Salesforce provides one channel that carries change events for every selected object:
/data/ChangeEventsA subscriber on /data/ChangeEvents receives events for each object you selected in Setup. Each event still carries its own ChangeEventHeader, so the subscriber can tell the events apart. The header field entityName holds the object's API name, such as Account or Invoice__c. Note that entityName uses the object name, not the change event name.
The trade-off is selectivity. You get everything you selected, and the subscriber must discard what it does not need. Every event also counts against your delivery allocation, whether your code uses it or not. Allocations are covered in a later chapter.
Custom channels
A custom channel is a channel you define yourself. It groups a chosen set of entities into one stream. Its name ends in __chn:
/data/SalesEvents__chnCustom channels let you give one subscriber only the objects it needs, without the all-objects channel. They also let you apply filters and enriched fields. Both topics come in a later chapter. For now, remember that a custom channel name ends in __chn, not ChangeEvent.
Summary of channel types
| What you want | Channel name | Example |
|---|---|---|
| One standard object | `/data/<Object>ChangeEvent` | `/data/AccountChangeEvent` |
| One custom object | `/data/<Name>__ChangeEvent` | `/data/Invoice__ChangeEvent` |
| One managed-package custom object | `/data/<ns>__<Name>__ChangeEvent` | `/data/acme__Invoice__ChangeEvent` |
| All selected objects | `/data/ChangeEvents` | `/data/ChangeEvents` |
| A chosen group of objects | `/data/<Name>__chn` | `/data/SalesEvents__chn` |
How the pieces relate
The diagram shows how one record change reaches several channels. The same event is published once and made available on the matching channels.
Building the name in code
Integration code often needs a channel name for an object it only knows by API name. A small helper keeps the rule in one place. This Apex snippet runs as written in Anonymous Apex:
public class ChannelNames {
public static String forObject(String objectApiName) {
String eventName;
if (objectApiName.endsWith('__c')) {
eventName = objectApiName.removeEnd('__c') + '__ChangeEvent';
} else {
eventName = objectApiName + 'ChangeEvent';
}
return '/data/' + eventName;
}
}
System.debug(ChannelNames.forObject('Account'));
System.debug(ChannelNames.forObject('Invoice__c'));
System.debug(ChannelNames.forObject('acme__Invoice__c'));The debug log shows /data/AccountChangeEvent, /data/Invoice__ChangeEvent, and /data/acme__Invoice__ChangeEvent. If you paste this into the Developer Console's Execute Anonymous window, define the class first in a separate step. Anonymous Apex cannot declare a top-level class in the same block as statements.
The helper handles only the standard-object and custom-object cases. It does not validate that the object exists or is selected for CDC. Share the rule in one place, not scattered across subscribers, so a naming mistake is fixed once.
Where to find the name for an object
You can confirm a channel name in three places.
- In Setup, open Change Data Capture and check the selected entities. The selected list shows which objects publish events.
- In an event itself, the
ChangeEventHeader.entityNamefield tells you which object the event came from. - In your own code, the trigger declaration compiles only if the change event type exists. A compile error on
on WidgetChangeEventmeans the object is not selected or the name is wrong.
The third check is quick. Saving a trigger on a missing change event type fails immediately, so you learn about a bad name before any record changes.
Common errors
- Wrong prefix. Using
/event/AccountChangeEventor/topic/AccountChangeEvent. Change events always use/data/. - Keeping
__con custom objects./data/Invoice__cChangeEventis wrong. The suffix__cis replaced, not extended. - Forgetting the namespace. A managed-package object needs its namespace in the channel name.
- Object not selected. The channel name is right, but the object is not enabled in Setup, so no events arrive.
- Using the entity name outside Apex. A subscriber given
AccountChangeEventwithout/data/cannot find the channel. - Confusing
entityNamewith the event name. The header saysAccount, but the channel isAccountChangeEvent.
Exercises
Exercise
A custom object has the API name Shipment__c. What is the channel name for its change events, and what is the name of the trigger's event type in Apex?
Show answer
The channel is /data/Shipment__ChangeEvent. The Apex trigger event type is Shipment__ChangeEvent, declared as trigger ShipmentChangeTrigger on Shipment__ChangeEvent (after insert). The __c suffix is replaced by __ChangeEvent.
Exercise
Your team subscribes to /data/ChangeEvents and wants to process only Contact updates. Which field in the event tells you the event is for Contact, and what value do you compare against? What is the cost of this approach compared with subscribing to /data/ContactChangeEvent?
Show answer
Read ChangeEventHeader.entityName and compare it to Contact. Also check ChangeEventHeader.changeType for UPDATE. The cost is that the subscriber receives events for every selected object, discards most of them, and still uses delivery allocation for all of them. Subscribing to /data/ContactChangeEvent delivers only Contact events.