Change Data Capture Salesforce

SoomjeetSahoo
3 min readApr 14, 2022

Platform Event on steroids

Change Event structure for this scenario

In this fast-paced world, business needs solutions to be fast and agile, and is no way different in Salesforce. Running some non-real-time processes in async and sparing resources of the current transaction to the real-time updates is the key to achieving the balance between conflicting changes.

One such tool in salesforce to achieve this is the ChangeDataCapture or commonly known as CDC or ChangeEvent. In this article I’ll be going through the What, Why, and a use case of the CDC. So, Let’s begin by wearing our salesforce hoodies.😎

ChangeDataCapture, as the name suggests, captures the change in the data. It creates a new transaction that fires after a record is pushed through some DML which creates a ChangeEvent record(In this Case AccountChangeEvent), and it further invokes the ChangeEvent trigger for the same object.

The thing to note here, this trigger context of ChangeEvent is executed via “Automated Process” (a default system user) and creates a separate transaction that is async but with synchronous limits to it.

With that said, go through Pros & Cons of Using CDC.

Pros:

Config Friendly to Setup:
Unlike Platform Events where setting up event Objects, Fields are real pain CDC setup is really easy and straightforward.

Go To Setup -> Quick Find -> Search for “Change Data Capture” (Under Integration Tab) -> Just move the entities from left to right on the Combo-box to activate -> Save

Really. That’s It!!

Reduces Trigger Complexity:
After DML trigger logic can be easily replicated in CDC. ThereBy reduces the burden on triggers.

Async Process :
Being Async has its advantages i.e. having a separate transaction to balance the heavy-lifting.

Faster Triggering Time:
All the vanilla async processes like Future, Queueable, or Batch are stored in a queue and are executed upon resource availability. While CDC is executed immediately as there are no async limits involved.

External Visibility:
Just like Platform Events, CDC uses the CometD to publish any changes on the channel, and systems subscribed to it get the info.

Cons:

No OnDemand Firing:
Unlike Platform Events, We can’t fire CDC as and when needed it reacts/triggers on DML operation on sObjects.

1MB message size limit:
The maximum size limit allowed per ChangeEvent Trigger fire is 1MB. To know more. Click here.

As Always please refer to the documentation to know more.

Now, let’s jump into the use case. I’ll keep it easy and straightforward.

Scenario
Business wants a Contact Record(Primary Contact) needs to be created with Name as “<Account Name> Primary Contact” and Phone as Account’s Phone as and when an account is inserted into SF. This solution also needs to factor in the update in the Account’s Phone and cascade the changes to all of its related Contacts.

Step 1: Enable CDC(Change Data Capture) For Account object.

Enabling Change Data Capture for Account Object

Step 2: Setting up the AccountChangeEventTrigger.

Trigger on the Account’s Change Event

Step 3: Setting up the AccountChangeEventHandlerClass.

To know how the ChangeEvent Message is Structured click here.

The reason why we have queried the records here is that the list of AccountChangeEvent doesn’t give us all the data that is necessary and tries to cram in as much data as possible(Atleast all the record Ids) due to 1MB message size limit. If it increases to more than 1 MB, Gap Event is created instead.

Well, I hope the above example helps you out with understanding the logic of how the CDC works and how to leverage the power of the CDC in solving business needs. 😊

--

--

SoomjeetSahoo

Salesforce Dev @ Salesforce with lots of inquisitiveness to learn and grow in Salesforce and Node Domains. Currently 15x Salesforce Certified & 14x Super Badge.