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”



RecordChangeEventHandlerController.js



  •  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.



RecordChangeCaptureController.js



RecordChangeCaptureHelper.js



RecordChangeCapture.design



  • 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

(Visited 632 Times)

2 thoughts on “Change Data Capture Events in Salesforce – Keep your UI updated with latest data

  1. Utkarsh

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *