Change Data Capture CDC Advance Configurations | Filters | Channel Members | Salesforce ⚡️


Hi there,

In this blog you will learn advance Change Data Capture configurations. This blog is related to one of my previous blog which was a basic version of Change Data Capture where we have use standard CDC events and in this blog I will show you how you can do custom configurations in it.

In this blog you will learn following things:

  • Connect Postman with Salesforce Org
  • Create Custom CDC Channel
  • Create Custom Channel Members
  • Use of Enriched Fields in CDC
  • Add Filter Criteria in CDC Events
  • How to call custom CDC Events


Why you need a CDC custom channel?

Custom channels are useful when there are multiple subscribers and each subscriber needs a different set of objects or fields.
Instead of subscribing to a standard channel which will trigger on all the field change you can create a custom CDC channel and add a filter criteria to trigger it on some specific field changes.
For example what if you are having 100 fields in your object but the subscriber needs notifications on the specific 20 fields?
In this scenario you can use custom CDC events with filter criteria.

How to create a Custom CDC Channel?

You can create a custom channel with Tooling API or Metadata APIs. As part of Metadata CDC will be available as the PlatformEventChannel and its members will be available as part of PlatformEventChannelMembers metadata.

In this blog we will be using Tooling APIs to create a custom channel.


How to set up Postman and Tooling API

To setup postman with your org you can follow below installation instructions:

Create a Custom CDC Channel

After connecting your Postman using above steps you can follow below steps to create a custom channel.

  1. To send a POST request to PlatformEventChannel, in your forked Salesforce API collection, expand Tooling.
  2. Select POST Post Tooling sObject.
  3. In the new tab that opens for this request, replace the <SOBJECT_API_NAME> placeholder in the URI with PlatformEventChannel. The resulting URI is: {{_endpoint}}/services/data/v{{version}}/tooling/sobjects/PlatformEventChannel
  4. Click Body and ensure that raw and JSON options are selected.
  5. Replace the body with this JSON body.
  6. Click Send
1
2
3
4
5
6
7
{  
  "FullName": "CDCEvents__chn",
  "Metadata": {
    "channelType": "data",
    "label": "Custom Channel for the Demo"
  }
}

After creating the channel you will be getting below response.



How to add Custom Channel Members and Enriched Fields?

As part of above custom CDC channel we will be adding a custom CDC ChannelMember as well. One channel can have multiple channel members.
While creating a channel member you can add enriched fields in it.

Enriched Fields: Enriched fields are kind of a config for fields which should be always there whenever a CDC request is being created. 

As part of this demo I have created below enriched field.



Now to create Custom Channel Member with above field as enriched field please follow below steps: 
  1. If you’re still on the PlatformEventChannel request page in Postman from step 1, stay on the same page and replace PlatformEventChannel at the end of the URI with PlatformEventChannelMember. Otherwise, perform these steps:
  2. Under Tooling, click POST Post Tooling sObject.
  3. In the new tab that opens for this request, replace the :SOBJECT_API_NAME placeholder in the URI with PlatformEventChannelMember. The resulting URI is: {{_endpoint}}/services/data/v{{version}}/tooling/sobjects/PlatformEventChannelMember
  4. Click Body and ensure that raw and JSON options are selected.
  5. Replace the body with this JSON body.
  6. Click Send
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "FullName": "CDCEvents_chn_AccountChangeEvent",
  "Metadata": {
    "enrichedFields": [
      {
        "name": "Account_Number__c"
      }
    ],
    "eventChannel": "CDCEvents__chn",
    "selectedEntity": "AccountChangeEvent"
  }
}


After creating the ChannelMember you will be getting below response.



Congratulations, you have created a Custom Channel and Custom Channel Member with an enriched field. 

How to add a filter in CDC?

The filter is going to be a custom field in your object based on which you will be able to add a condition on when CDC should trigger.

For this demo I have created below field in my Account object: 



Now above created field I will be adding in my CDC channel member, but this time it is going to be a PATCH request and the channel member is already there and we just need to update it.

Query the Channel ID
  1. In Postman, under Tooling, select GET Tooling Query.
  2. In the Params tab, replace the value in the row for q with: SELECT Id FROM PlatformEventChannel WHERE DeveloperName='SalesEvents'. The full URI looks as follows: {{_endpoint}}/services/data/v{{version}}/tooling/query/?q=SELECT Id FROM PlatformEventChannel WHERE DeveloperName='CDCEvents'
  3. Click Send.
  4. The returned response includes the channel ID in the Id field and looks similar to the following example. Copy the Id value to use it in the next step.


Query the Channel Member ID
  1. In Postman, under Tooling, click GET Tooling Query.
  2. In the Params tab, replace the value in the row for q with the following value and replace <channel ID from query> with the Id you obtained from the previous step: SELECT Id,DeveloperName,EventChannel,SelectedEntity FROM PlatformEventChannelMember WHERE EventChannel='<channel ID from query>' AND SelectedEntity='AccountChangeEvent'.  The full URI looks as follows: {{_endpoint}}/services/data/v{{version}}/tooling/query/?q=SELECT Id,DeveloperName,EventChannel,SelectedEntity FROM PlatformEventChannelMember WHERE EventChannel='<channel ID from query>' AND SelectedEntity='AccountChangeEvent'
  3. Click Send.
  4. The returned response includes the channel member Id in the Id field and looks similar to this example. Copy this Id value to use it in the next step.


Make a PATCH Request on PlatformEventChannelMember
  1. If you performed a POST request previously in Postman, stay on the same request page and ensure that the URI ends with /PlatformEventChannelMember. Otherwise, perform these steps:
  2. Under Tooling, click POST Post Tooling sObject.
  3. In the new tab that opens for this request, replace the :SOBJECT_API_NAME placeholder in the URI with PlatformEventChannelMember. The resulting URI is: {{_endpoint}}/services/data/v{{version}}/tooling/sobjects/PlatformEventChannelMember
  4. Append a forward slash (/) followed by the channel member Id you copied in the previous step to the end of the URI. The resulting URI is similar to this example except for the Id value that could be different in your case: {{_endpoint}}/services/data/v{{version}}/tooling/sobjects/PlatformEventChannelMember/0v8B0000000PBNFIA4
  5. Change POST to PATCH.
  6. Click Body and ensure that raw and JSON options are selected.
  7. Replace the body with this JSON body.
  8. Click Send.
  9. Verify the response status is 204 No Content.


Awesome, so now you have added your field as filter criteria in your custom CDC Event. Now you can make the field true based on some specific conditions to trigger your CDC Event conditionally.

Checkout complete video tutorial below


 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments