Sub topic 11
Custom channels and selecting entities
This chapter assumes you can enable Change Data Capture for an object, know the default /data/AccountChangeEvent channel naming, and can deploy metadata with Salesforce CLI.
Why use a custom channel
The default channels give you one channel per object, plus /data/ChangeEvents, which carries every object you enabled. That leaves two awkward options for a subscriber that cares about only a few objects:
- Open one subscription per object. Each subscription is a separate stream, so the subscriber has to merge them.
- Subscribe to
/data/ChangeEventsand discard what it does not need. The subscriber still receives and processes every event.
A custom channel is a channel you define, with exactly the objects you pick. One subscription then receives changes for all of them, in one stream.
Define the channel and its members
You create a custom channel with two metadata types. PlatformEventChannel defines the channel. PlatformEventChannelMember adds one entity to it.
The channel name must end in __chn. Set channelType to data for change events.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannel xmlns="http://soap.sforce.com/2006/04/metadata">
<channelType>data</channelType>
<label>Sales Changes</label>
</PlatformEventChannel>Save this as force-app/main/default/platformEventChannels/Sales_Changes__chn.platformEventChannel-meta.xml.
Add one member file for each object. The selectedEntity value is the change event name of the object. For a standard object it is AccountChangeEvent. For a custom object Invoice__c it is Invoice__ChangeEvent.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>Sales_Changes__chn</eventChannel>
<selectedEntity>AccountChangeEvent</selectedEntity>
</PlatformEventChannelMember>Save this as force-app/main/default/platformEventChannelMembers/Sales_Changes_chn_AccountChangeEvent.platformEventChannelMember-meta.xml. Repeat it with ContactChangeEvent and Invoice__ChangeEvent for the other members. Then deploy:
sf project deploy start --source-dir force-app/main/defaultSubscribe to the custom channel
The channel name takes the form /data/ followed by the channel API name. A subscriber only changes the channel string. Here is the earlier Lightning web component with the new name:
channelName = '/data/Sales_Changes__chn';Each message keeps the normal structure. Because one channel now carries several objects, read ChangeEventHeader.entityName to see which object changed:
subscribe(this.channelName, -1, (message) => {
const header = message.data.payload.ChangeEventHeader;
if (header.entityName === 'Invoice__c') {
console.log('Invoice changed', header.recordIds);
}
});Choosing between channel types
| Need | Channel |
|---|---|
| Changes for one object | `/data/AccountChangeEvent` |
| Changes for every enabled object | `/data/ChangeEvents` |
| Changes for a chosen set of objects | Custom channel such as `/data/Sales_Changes__chn` |
Common errors
- Deployment fails on the member. The
selectedEntitymust match a change event that exists, so check the spelling and the__ChangeEventsuffix on custom objects. - No events arrive. Confirm the channel name ends in
__chnand that the subscriber uses the/data/prefix. - Wrong events handled. A multi-object channel needs a check on
entityNamebefore the subscriber applies object-specific logic.
Newer API versions let a channel member also carry a filter expression and enriched fields. Check the current Metadata API reference for your org's version before relying on them.
Exercise
You need one subscriber to receive changes to Account and to the custom object Order__c. What are the channel name pattern, the two selectedEntity values, and the header field the subscriber uses to tell the objects apart?
Show answer
Use a channel like /data/Orders_And_Accounts__chn, defined by a PlatformEventChannel with channelType set to data. Add two PlatformEventChannelMember files with selectedEntity values AccountChangeEvent and Order__ChangeEvent. The subscriber reads ChangeEventHeader.entityName to tell Account from Order__c.