CDC - Change Data Capture in Salesforce | LWC Stack ☁️⚡️


What you will learn today?

  • What is CDC
  • CDC Stages
  • What is the use of Streaming Events
  • When You should use it
  • CDC Process Flow
  • How to Enable CDC
  • Live Demo with Custom & Standard Object

What is CDC?

Change data capture is a streaming product using which you can integrate your Salesforce Data with external system. Using change data capture you will be able to receive changes in real time to your external system. 
It will be publishing event for your operations in salesforce like
  1. Create
  2. Update
  3. Delete
  4. Undelete

You can consider CDC as  a real time data replication process which will include following stages.



Use of Streaming Events

Streaming Events are instant push notifications, where one system will be publisher/sender of the event and the other system will be a subscriber/receiver.
This is a push technology and opposite to standard way of requesting data using pull technology.
With pull technology clients request data from the server whenever is required but in this push technology CDC sends notifications to subscribers whenever a data changes in Salesforce.



When you should use CDC?

There could be multiple ways to solve a problem but you should be aware of the best option.

  • Receive notifications of Salesforce record changes, including create, update, delete, and undelete operations.
  • Capture changes of most fields for all records.
  • Get information about the change in the event header, such as the origin of the change, so you can ignore changes that your client generates.
  • Perform data updates using transaction boundaries when more than one operation is part of the same transaction.
  • Use a versioned event schema.
  • Subscribe to mass changes in a scalable way.
  • Get access to retained events for up to 3 days.

CDC Process Flow




So whenever you will be having any Create, Update, Delete and Undelete optiorations in your Salesforce org, CDC will capture it and pass it to its subscriber.

How to Enable CDC?

Step 1: Go to Setup and search for Change Data Capture.

Step 2: Move your objects for which you want to enable CDC from available entities box to selected entities as shown in below image.




Live Demo

In below video I have enabled it for a Custom and Standard event and subscribe to it using a emp connector. 

EMP Connector Code Snippet



Sample JSON Payload

Create

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
   "LastModifiedDate":"2023-05-01T09:06:31Z",
   "OwnerId":"005B0000008K0RJIA0",
   "CreatedById":"005B0000008K0RJIA0",
   "ChangeEventHeader":{
      "commitNumber":10963730219094,
      "commitUser":"005B0000008K0RJIA0",
      "sequenceNumber":1,
      "entityName":"HR_Account__c",
      "changeType":"CREATE",
      "changedFields":[
         
      ],
      "changeOrigin":"com/salesforce/api/soap/58.0;client=SfdcInternalAPI/",
      "transactionKey":"00053a5f-5169-7296-40b7-c163f7608716",
      "commitTimestamp":1682931991000,
      "recordIds":[
         "a01B0000007hUQSIA2"
      ]
   },
   "CreatedDate":"2023-05-01T09:06:31Z",
   "First_Name__c":"Raj",
   "LastModifiedById":"005B0000008K0RJIA0",
   "Name":"Test 2",
   "Last_Name__c":"Kapoor"
}

Update

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{
   "LastModifiedDate":"2023-05-01T09:04:43Z",
   "ChangeEventHeader":{
      "commitNumber":10963729529953,
      "commitUser":"005B0000008K0RJIA0",
      "sequenceNumber":1,
      "entityName":"HR_Account__c",
      "changeType":"UPDATE",
      "changedFields":[
         "LastModifiedDate",
         "Last_Name__c"
      ],
      "changeOrigin":"com/salesforce/api/soap/58.0;client=SfdcInternalAPI/",
      "transactionKey":"00053a46-20f5-9bc0-530f-b10c395f9c17",
      "commitTimestamp":1682931883000,
      "recordIds":[
         "a01B0000007hUQNIA2"
      ]
   },
   "Last_Name__c":"Kumar"
}


Delete

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
   "ChangeEventHeader":{
      "commitNumber":10963730637545,
      "commitUser":"005B0000008K0RJIA0",
      "sequenceNumber":1,
      "entityName":"HR_Account__c",
      "changeType":"DELETE",
      "changedFields":[
         
      ],
      "changeOrigin":"com/salesforce/api/soap/58.0;client=SfdcInternalAPI/",
      "transactionKey":"00053a72-75f5-70be-88ed-1b042297f7ea",
      "commitTimestamp":1682932074000,
      "recordIds":[
         "a01B0000007hUQNIA2"
      ]
   }
}

Checkout complete video 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