Benefits of Creating a Custom Salesforce Change Data Capture Channel

Author: Bruce Tollefson Published: July 25, 2021; Modified: May 9, 2022
connected_dots

Salesforce Change Data Capture is very useful in being able to capture and replicate changes that occur with a record. Each change creates a CDC event message. The message can either be one or multiple record changes with a given transaction creating multiple changes. This article assumes you know a little bit about Salesforce change data capture will not be covering CDC the uses, the different types, and their meanings. Instead, the focus will be on creating a custom change data capture channel and the benefits of creating a custom channel.

API v47.0 brought the PlatformEventChannel and PlatformEventChannelMember objects. These create a new way to be able to use Change Data Capture. Previously each object had its own channel creating an independent channel for events. This can create issues if one of the two objects need to be sequenced before the other. If they are being subscribed with independent channels 1 has the potential to be processed before the other. As an example Task and TaskRelation. The taskrelation has a child relationship to the opportunity and we want to get the task event before the taskrelation.

For example, we will be using the platform event viewer LWC and I will be using postman and the tooling api to create the platformeventchannel and platfromeventchannelmember. You could also use the metadata api for channel and member. You can find the Salesforce documentation on standard and custom channels here.

Create Custom Channel and Members

Using the tooling api with postman I created the channel:

When creating the channel use ‘__chn’ at the end of the fullname, the channelType specifies the type of the event, currently only change data capture events are supported, use ‘data’. Next create the task platformeventmemberchannel:

Both the eventChannel and the selectedEntity need to be specified. Note the Fullname only has one ‘_’ if you use two you will receive an error. Then do the same for the taskrelation. Now you have a custom channel that can be subscribed to and will bring in the Task and TaskRelation in the same channel.

To test use the subscriber LWC and put ‘/data/CustomTaskEvents__chn’ into the channel name and subscribe. Then in dev console go to anonymous and put in and execute:

Contact contact = [Select Id from Contact Limit 1]; Task tsk = new Task(Subject='Test Custom Channel', WhoId = contact.id); insert tsk;

This will create 2 events one for the Task(this will come first) and another for the TaskRelation(second):

Event Enrichment

If you would like to enhance the channel even further within custom channels you are able to specify fields that you would like to always be added to the event message. The Salesforce documentation for event enrichment can be viewed here. As an example update events only provide the updated field however you can add additional fields that will always be added to the message. Such as an external Id. This will be demonstrated by using the whoId field on the Task. Delete the Task channel member by querying in dev console:

SELECT Id, DeveloperName, EventChannel, SelectedEntity FROM PlatformEventChannelMember

Then click the delete row button. Next add the channel member again:

Now try updating the task you previously created and you will see the whoId will be added. When using CDC to sync/replicate data using an external id field that other systems can use as an identifier can be extremely beneficial for data integrity.

2 comments on “Benefits of Creating a Custom Salesforce Change Data Capture Channel”

  • HI Hairstyles says:

    This actually answered my problem, thanks!

  • Leave a Reply

    Your email address will not be published. Required fields are marked *