Change Data Capture in Salesforce

There are many ways of asynchronous processing in Salesforce like the Future method, Queueable Apex, Batch job, Scheduler Apex and etc. In Summer 19 release, Salesforce provides another feature called “Asynchronous Apex Triggers” that can process data asynchronously with the help of Change Data Capture.

What is Change Data Capture Events?

Change Data Capture is a streaming product on the Lightning Platform that enables you to efficiently integrate your Salesforce data with external systems. Change data capture is also called CDC. With Change Data Capture, you can receive changes in Salesforce records in real time and synchronize corresponding records in an external data store. It is an outbound process. It supports CRED and undeletes operations.

Change Data Capture (CDC) Key Points

Change Data Capture (CDC)

When to Use Change Data Capture (CDC)

  • Initial copy of an entire set of data is already in the external system.
  • Now, You want to have continuous synchronization of new and updated data to the external system.
  • Reconciliation of duplicate data between the two systems.
  • It publishes the delta of Salesforce data, whether for new records or changed records.
  • It ignores sharing setting and sends change events for all records, but It respects field-level security.
  • If salesforce record fields are encrypted with Shield Platform Encryption, changes in encrypted field values generate change events.

A possible way to subscribe to Change Event is as follows 

  • Asynchronous Trigger 
  • Lightning Component 
  • Lightning Web Component 
  • EMP Connector

What are Asynchronous Apex Triggers?

A change event trigger is an “after-insert” trigger. The trigger fires after the change event message are published. You can create an after-insert trigger in the Developer Console the same way you create an Apex object trigger after enabling the object for Change Data Capture.

Enable Change Data Capture

To enable the objects for receiving change notification follow below step.

  • Setup-> Integrations -> Change Data Capture.
What are Asynchronous Apex Triggers?
  • Select the object in Selected Entities
  • Then click Save.

Create Asynchronous Apex Triggers

Let’s see how we can create the Asynchronous Apex Triggers. You can create an after-insert trigger in the Developer Console the same way you create an Apex object trigger. When you create a trigger, pick the change event object from the SObject dropdown, which is populated with the change events available in your org.

You can create an after-insert trigger in the Developer Console the same way you create an Apex object trigger. When you create a trigger, pick the change event object from the sObject dropdown, which is populated with the change events available in your org.

Asynchronous Apex Triggers Example

trigger AsyncAccountTriggers on AccountChangeEvent (after insert) {

}

Apex Trigger for Changed Events

Like Apex Trigger for any Salesforce Objects, we can define a trigger or Change Event corresponding to Salesforce Object. It fires when the Change event is received. It runs Asynchronously after the database transaction is completed. It is ideal for resource-intensive business logic while keeping transaction-based logic in the object trigger. It runs under the Automated Process entity.(You can check in Debug logs). It is subjected to Apex synchronous governor limits. Max batch size of 2,000 event messages in Trigger.New.

Asynchronous Apex Triggers Consideration

Some Important point to consider :-

  1. Here is list of supported object
  2. For a standard object, the change event name is StandardObjectNameChangeEvent for example AccountChangeEvent
  3. For a custom object, the change event name is CustomObjectName__ChangeEvent for example Employe__ChangeEvent (suffixed as __ChangeEvent) 
  4. Asynchronous Apex triggers are change event triggers that run asynchronously after a database transaction is completed
  5. Available for After Insert Event only
  6. With Asynchronous Apex Trigger we can move complex computations and logic out of the transaction 

Change Data Capture Video

YouTube video

Best Use Cases of Change Data Capture (CDC)

Consider this as an out-of-the-box event driven feature on Salesforce lightning platform enabling you to integrate data with external systems in real-time. For any custom publish-subscribe use cases take a serious look at Platform Events.

  • CDC Use Case 1 – Keep your Salesforce data, say changes in account or opportunity up-to-date in your AWS Redshift Data Warehouse in real-time.
  • CDC Use Case 2 – Replicate a Salesforce data change, in real-time, say a change in account or opportunity or lead, into data tables of your external applications such as AWS SageMaker, SAP order management system, or Oracle Financials.
  • CDC Use Case 3 – Kick off a certain logic in your external application, say SAP order management, in reaction to a sold product or a won opportunity in Salesforce.
  • CDC Use Use 4 – Archive Salesforce data in AWS S3 every time a certain Salesforce data change occurs, say an activity record or case record or user record is getting deleted.
Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 460

2 Comments

  1. Hi Amit,
    We have a Contact CDC event trigger capturing changed contacts data and then syncing that data to the corresponding community user records. We have noticed that the async trigger works fine if we are doing updates directly from Salesforce or using data loader. But the trigger doesn’t fires or doesn’t updates user records sometimes when the bulk contact update job comes via integrations. In contact bulk load jobs we often notice there are failures with row Lock issue. And this issue of user records not getting updated is intermittent. Request your advice on what could be the probable issue.

Leave a Reply

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