Change Data Capture Events in Salesforce – Keep your UI updated with latest data

Change Data Capture Events in Salesforce – Keep your UI updated with latest data

Change Data Capture(CDC) Events – This is still a new term in the Salesforce world. Do you know what it is and how powerful is it? If not, read this post and learn CDC events.

  • CDC events are basically used to track the changes in a Salesforce record. This means, it tracks create, update, delete and undelete changes of a Salesforce Record, and fires an event when the change occurred.
  • You can subscribe to CDC channel to listen for these events. You can even subscribe to these events from you your own platform using javascript libraries like cometD.
  • Once you subscribe to these events, you will get a notification whenever a new event is generated, the notification will include a payload in JSON format which will have all the details about the event like:
    • SObject name – where the change happened like Account, Opportunities etc
    • Record Ids – List of record ids which are changed
    • Change Type – Create/Update/Delete/Undelete
    • Changed Fields – Fields which are modified
    • User – The user who performed the change
    • Timestamp – When the change has happened
  • You can use this payload to perform your operation. For example, if you want to keep your in-house application in sync with Salesforce Data, you can use payload information to update your application with data changes happened in Salesforce.

Sample Payload

CDC Events tracks all changes and all fields in an object and if any of the fields are modified the event will be triggered. This makes it very easy to listen for all type of changes in a record. You can use Streaming API for a similar purpose, but there you specifically need to specify which fields and change type you want to track while creating PushTopics.


Implementation Example

Let’s discuss an implementation scenario of CDC events where you want to keep your UI updated with latest changes. 

  • User “Ron Weasley” is viewing an Opportunity record which is being viewed by the user “Harry Potter” as well at the same time.
  • Harry Potter has changed the record data and has modified Opportunity amount.
  • Ron Weasley who is unaware of this change and still viewing the old record, has to make a very important decision based on Opportunity amount and send the same to higher management.
  • Now in this situation, Ron Weasley may take an incorrect decision and send incorrect data to higher management.

How to avoid the above situation? Well, lets leverage Change Data Capture events and lightning:empApi component to track changes in Opportunity Record, and update the data on Ron Weasley’s screen as soon as the data is modified by Harry Potter. Our coding solution will include below components:

  • RecordChangeEventHandler Component – The main component to subscribe, unsubscribe and receive messages from streaming channel of Change Data Capture events. Once this component receives a message, this will fire a component event, which would be handled by the parent component. The channel name is like below:
    • Standard Object: “ChangeEvent” appended in the end. Example, AccountChangeEvent, OpportunityChangeEvent etc.
    • Custom Object: “__ChangeEvent” appended in the end. Like for custom object “Car__c”, the change event channel name would be “Car__ChangeEvent”


  •  RecordChangeEvent Component Event – This event will be fired by RecordChangeEventHandler component whenever it receives a message from Change Event. This component event will be handled by the parent component.

  • RecordChangeCapture – This component is parent component which will include actual markup and handle the above-mentioned component event fired my RecordChangeEventHandler component.



  • SupportedObjectsForChangeEvents.js

  • RecordChangeCaptureLightningController.apxc

There is a lot going on in this code. To understand above code line by line, watch below video where I explained the usage of Change Data Capture events and above code along with output.


Change Data Capture are not Generallly Available yet and are only available in developer orgs and Sandboxes. Refer Change Data Capture Developer Guide for more details information.

Reference: Change Data Capture Developer Guide

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 9 Comments

  1. Hi Manish thanks for the informative video.
    lightning:empAPi worked when included in the lightning component but same did not work when included in lightning app.
    I tried to include compnent ion app/ and alternatively tried to have same component code in lightning app but did not work.
    I am trying to create a lightning cti softphone and require static address for softphone , that is why using app.

  2. Hi Manish,
    Will this concept works if I did Record changes using DataLoader?


    1. Hi Damargunta,

      Yes, it will work on the records updated using data loader as well.

  3. Hi Manish,

    How to check the condition with newly updated field value ..lets say we we have account phone changed from 123 to 1234567 then how to get the filed value for if condition..

    1. You can get this from payload. The payload you receive has all the changed fields with their label and latest value.

  4. Wow, nice work! This is really cool functionality you have built.

  5. Hi Manish,
    I tried this but however, it is not working for me. Somehow, I am receiving the error in my RecordChangeEventHandler.js
    error message : “message is not defined”
    I am very new to CDC, so don’t know the exact cause.
    Any help would be appreciated.

    1. It could be the payload. Please check if the message property has been changed in payload or if the payload structure has been changed.

Leave a Reply

Close Menu