Salesforce Change Data Capture - Asynchronous Triggers and Subscription using MuleSoft & CometD
In this post, let us see what is Change Data Capture, when to use them and the different subscription approaches.
1. What is Change Data Capture
Change Data Capture is similar to Platform Events. This can be used to keep your external system in sync with Salesforce data. It is supported for all custom Objects and some standard Objects.
The supported standard object list can be found here.
Once enabled for a particular object, whenever that object record is created, updated, deleted or undeleted, an event will be fired to the event bus. This fired event will be available for 72 hours on the bus and an external application can subscribe to this.
How it is different from Platform events?
1. In platform events, we can define our own fields and structure whereas CDC is applied on existing Objects
2. You can publish platform events from Flows, Apex, and process but CDC is fired by default on record changes
How to Enable CDC?
Go to setup->Integrations->Change Data Capture
Here you can select Objects. Please note, by default you can select only 5 objects. If we need to capture more objects we need to purchase an additional license.
2. CDC Event Structure
The CDC Object is always called <Objectname>ChangeEvent
Example - AccountChangeEvent, ContactChangeEvent,customObject__cChangeEvent
Below is the structure of CDC event:
Header Field Details:
Body Field Details:
3. Subscribe using Trigger
One of the advantage of CDC is that, you can create an after Insert Trigger on ChangeEvent record.
Advantage of this approach:
Use Case:
When ever a new contact is getting created/updated with Country field value, parent Account's Account country field should be appended with new country
If Account ABC has 2 contact - Contact X with country India and Contact Y with country France, the Account Country in Account ABC should be updated as India,France
Solution Approach using CDC:
1. Enable CDC on Contact
We have done this already.
2. Create Trigger on ContactChangeEvent to process Data
Trigger Logic:
Note- below code is not containing complete logic and not following trigger handler best practice.
You can see that Trigger is on ContactChangeEvent and the operation is always after Insert
To enable debug log for Asynchronous trigger, we need to enable it on Automated Process as hsown below:
Account Record:
You can see Account country is empty.
Now let us insert a new contact - Contact X with country as India.
You can see Account Coutry got updated as India now:
In the debug you can see an entry for automated process:
The record on ContactChangeEvent looks like below:
4. Subscribe using CometD Client
This blog details how you can subscribe to events using CometD Client. Using that approach, I subscribed to ContactChangeEvent, and you can see events getting subscribed in CometD Client:
Subscibe using below command:
java -classpath target/emp-connector-0.0.1-SNAPSHOT-phat.jar com.salesforce.emp.connector.example.LoginExample <username> <password+securitytoken> /data/ContactChangeEvent
When w execute this, you can see events are getting subscribed, which got published in the last 72 hours:
5. Subscibing from Mulesoft Anypoint Platform
Subscribing to CDC events from Mulesoft Anypoint platform is easy. You can use a 30 day trial version.
1. Install Anypoint Studio from here
2. Once installation completed, run AnypointStudio.exe :
Now you will be able to add additinal logic in AnypointStudio to transform the received payload and load it to any oter system
6. Considerations
CDC will not be generated for below changes:
Concurrent Client Allocation
Default Allocation Details
References:
https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_when_to_use.htm
https://help.mulesoft.com/s/article/How-to-Subscribe-to-Salesforce-Change-Data-Capture-with-Mule-4
https://docs.mulesoft.com/salesforce-connector/0.3.8/
Nice and good article
ReplyDeleteCPQ Salesforce Training
Salesforce CPQ Course
Excellent article and with lots of information. I really learned a lot here. Do share more like this.
ReplyDeleteBlue Prism Training in Chennai
UiPath Course in Chennai
Blue Prism Course in Chennai
UiPath Online Course
Great Post!!! thanks for sharing with us.
ReplyDeletethe importance of google analytics
google analytics significance
Excellent article and thanks for sharing!!
ReplyDeleteJava Training in Kolkata
JavaTraining in Hyderabad
Flawless Writing and good content and thank you posting!!
ReplyDeleteSelenium Training in Mumbai
Selenium Training in Kolkata
nice ..................!
ReplyDeletemulesoft training
pega training
servicenow training training
Very useful blog
ReplyDeleteCDC Application Process
nice blog thanks for sharing
ReplyDeleteCDC Application Process