Pages

Showing posts with label external source. Show all posts
Showing posts with label external source. Show all posts

Wednesday, May 20, 2020

Part II : Change Data Capture ... Learn How To Subscribe Change Data Capture to External Source

In my previous blog, I have discussed Salesforce Change Data Capture and how this feature helps to publish change events that represent changes to records. I have taken an example to display change event records or messages on the Lightning Web Component using Platform Events. If your interested to have a look at my previous blog, please feel free to visit on below link:

Understanding Change Data Capture using Asynchronous Apex Triggers and Handling Platform Event in Lightning Web Components

In this blog, we will discuss how we can subscribe to Change Data Capture events and get the changed records that we can easily import to the external source.

To explain this I will be taking MuleSoft as a middleware tool that receives the message from the Subscribe Channel from salesforce and convert the message to the required format of another tool, so I will be passing change event records to MuleSoft using Subscription Channel and I will be fetching all the changed Case records in MuleSoft.

First, we need to set up the Change Data Capture and select the object for which we need to receive notifications for record changes. For my example, I will be selecting the Case object.


Once you have selected the object and saved it, salesforce will create a Subscription Channel with a name as CaseChangeEvent. To understand more on Subscription Channel, please click here. Also, in the previous blog, I have discussed little regarding Subscription Channel and naming conventions.

We are all done from the Salesforce side, now we need to move to the Mule platform and see how we can subscribe to the streaming channel provided by Salesforce.

So, in MuleSoft we will be selecting Salesforce connector and we will be using the Subscription Channel component, to receive events from Salesforce.
We will do the required Connector Configuration and provide the Streaming channel name. In this example, I have used the 'Basic Username and Password' connection and the channel name will be '/data/CaseChangeEvent'.

Below will be the MuleSoft flow:


Configuration for Subscriber Channel component and Salesforce Connector:








Now to read the response from the subscribed channel we will be using the Transform Message component and Logger component to generate the log for the payload which we have received from the channel.


Here, you can transform the payload into JSON object or Array of an object as per your convenience or whatever format is supported by another tool where you want to import the data.



Finally, its time to see the payload which we have got in the logger.


If you see the payload carefully which we have received it gives the complete information about the record that is changed in salesforce. Please refer to the change event message structure.


{
  "data": {
    "schema": "<schema_ID>", 
    "payload": {
      "ChangeEventHeader": {
         "entityName" : "...",
         "recordIds" : "...",
         "changeType" : "...",
         "changedFields": [...],
         "changeOrigin" : "...",
         "transactionKey" : "...",
         "sequenceNumber" : "...",
         "commitTimestamp" : "...",
         "commitUser" : "...",
         "commitNumber" : "..."
      }, 
     "field1":"...",
     "field2":"...",
     . . .
    }, 
    "event": {
      "replayId": <replayID>
    }
  }, 
  "channel": "/data/<channel>"
}

Please check out the descriptions of the fields which change event message contains from the below link:
Change Event Message Structure

So instead of doing periodic exports or imports of data or repeated API, we can easily make use of Change Data Capture to update in the external system. Capturing changes with Change Data Capture event notifications ensures that your external data can be updated in real-time and stays fresh.

I hope this will help to understand how we can subscribe to the change events from Salesforce using Change Data Capture to External Source.

References:


Looking forward to everyone's suggestions and comments!!!