Loading...

Real-time data is no longer a luxury; it’s a necessity for modern Salesforce-driven businesses. Whether it’s syncing records instantly, updating user interfaces without delays, or building event-driven architectures, organizations are increasingly relying on Salesforce Change Data Capture (CDC) to stay ahead.

In our previous blog on How to Use Salesforce Change Data Capture (CDC) in LWC for Real-Time UI Sync Without SOQL,” we explored how CDC enables seamless frontend updates.

Now, taking it a step further, this blog dives into Real-Time Salesforce Data Integration Using CDC and Apex, focusing on how developers can process change events efficiently using Asynchronous Apex Triggers.

By the end of this guide, you’ll understand how to leverage CDC in Apex, avoid common pitfalls like infinite loops, and build scalable, real-time integrations without relying on traditional polling or SOQL-heavy approaches.

Understanding CDC in the Context of Apex

As discussed earlier, Change Data Capture (CDC) allows Salesforce to publish real-time change events whenever records are created, updated, deleted, or undeleted. These events can be consumed by external systems, Lightning components, or Apex triggers.

While LWC focuses on UI-level synchronization, Apex takes CDC further by enabling backend processing and integration logic, making it a powerful tool for building event-driven systems within Salesforce.

This naturally brings us to the concept of Asynchronous Apex Triggers, which are specifically designed to handle CDC events.

What Are Asynchronous Apex Triggers?

To process data asynchronously, Salesforce already offers multiple approaches such as Future Methods, Queueable Apex, and Batch Apex. However, with the Summer ’19 release, Salesforce introduced a new mechanism – Asynchronous Apex Triggers using Change Data Capture.

These triggers are fundamentally different from traditional Apex triggers:

  • They always run asynchronously

  • They are invoked only after the database transaction is completed

  • They operate on change events instead of actual records

In simple terms, instead of reacting immediately within a transaction, CDC triggers respond after the change is committed, ensuring better performance and scalability.

Because change events are based on platform events, they share characteristics for subscription with Apex platform event triggers. Also, change event messages in triggers contain header and record fields and some additional fields not present in JSON event messages.

Syntax for Trigger: 

An example is given for Contact.

trigger ContactChangeEventTrigger on ContactChangeEvent (after insert) {

}

Since it is an Event and EventTrigger , it can run the maximum batch size of 2000 (the number of items in Trigger.new)..

And, as it is a Trigger, you can’t do callouts in this either.

You don’t have control; let’s say in ContactChangeEvent you update the contact’s field, and it will again fire a new ContactChangeEvent CDC Event. Infinite Loops will be there. You can prevent infinite loops in CDC using various ways, like. Few options are given below:

  1. Use a flag field (checkbox).

  2. Process only when relevant fields are changed.

  3. Check ChangeEventHeader.changeOrigin.

  4. Compare values before updating the record.

Understanding Apex Change Event Messages

Every CDC event received in Apex contains structured data similar to an SObject, including both record fields and metadata (header information).

Field Behavior Based on Operation

  • Create Operation

    When a new record is created, the event includes all fields, whether they contain values or not. This also includes default and system-generated fields like CreatedDate.

  • Update Operation

    For updates, only the modified fields contain values, while unchanged fields appear as null. System fields like LastModifiedDate are included, and LastModifiedById appears only if it has changed.

  • Delete Operation

    When a record is deleted, all fields in the event are null, as the record no longer exists.

  • Undelete Operation

    In case of undelete, the event includes the complete record data, similar to a create event.

Working with ChangeEventHeader

Each change event includes a header that provides metadata about the change. Each change event captured in the trigger contains also has record fields.

To obtain a header field, access the ChangeEventHeader field on the event object. For example, this code snippet gets the change event header and writes header field values to the debug log.

EventBus.ChangeEventHeader header = event.ChangeEventHeader;

String changeEntity = header.entityName;

String changeOperation = header.changeType;

List<String> changedFieldsList = header.changedFields;

Whereas,

entityName represents the SObject Name on which the action performed, and 

changeType represents the Operation performed like Create, Update, Delete and Undelete.

nulledFields contains the names of fields whose values were changed to null in an update operation. Use this field in Apex change event messages to determine if a field was changed to null in an update and isn’t an unchanged field.

changedFields contains the list of record fields that were changed in an update operation.

recordIds contains the list of all record IDs that have changed.

Now, let’s have a complete example to show the process.

For example, we will be using a Contact object. So, first enable CDC for Contact Object through setup as below:

 

CDC APEX 1


Now create a Trigger on Contact as below:

 

CDC APEX 2


Handler class of above Trigger:

 

CDC APEX 3

How to See Debug Logs for CDC Event Trigger

To see debugs, you have to create a new debug log with different settings, as the debug logs for CDC Event Trigger don’t show up directly in debug logs.

Follow the below settings to create new debug logs:

Setup -> debug logs -> New -> Select options as below in image -> Save.

CDC APEX 4


Now, you can see debug logs for the CDC event Trigger.

The debugs for Update Case will look like below.

CDC APEX 5


For the Create Case as below.

 

CDC APEX 6

No Data Found.

Related Articles
Salesforce Web Console Beta A Unified Browser Based IDE for Faster Development

Salesforce Web Console (Beta) is a browser-based development environment that allows developers to write Apex code, run queries, debug applications, and manage metadata directly within their org. It is designed as a modern alternative to older tools such as Developer Console and Workbench. The Salesforce Web Console is available directly inside Salesforce and does not […]

Read More
Fetch Document from Niko Health and Attach it to Salesforce Account

Integrating external healthcare platforms like Niko Health with Salesforce helps streamline document management and improve operational efficiency. In this guide, we will walk through the complete process of fetching a document from Niko Health using APIs and attaching it to a Salesforce account as a file. This step-by-step implementation is useful when you want to […]

Read More
Winter 26 Update Using LWC as Local Actions in Screen Flows

The Salesforce Winter Release 2026 introduces powerful AI-driven updates focused on automation, productivity, and smarter CRM experiences. From major enhancements in Salesforce LWC to improved Salesforce Flow local action capabilities, the release helps developers and admins build faster, more scalable Salesforce solutions. The Salesforce Winter 26 release also reflects Salesforce’s growing focus on AI-powered workflows, […]

Read More
Automation Difference Between HubSpot and Salesforce Flow

Customer Relationship Management (CRM) systems help businesses manage customer interactions, sales processes, and marketing campaigns efficiently. Two of the most popular CRM platforms used worldwide are HubSpot and Salesforce. Both platforms provide automation tools that allow businesses to automate repetitive tasks, improve efficiency, and reduce manual work. However, the automation capabilities and design of these […]

Read More
Automating Customer Approval Using Salesforce Approval Process

In many financial or compliance-based applications, organizations need a structured way to review and approve important documents or financial data before they are finalized. For example, a company implemented a Salesforce approval process to validate Customer’s ITR (Income Tax Return) details to ensure that high-income cases are reviewed by senior authorities while keeping the process […]

Read More
How to Automatically Create Messaging Consent Using Record Triggered Flow in Salesforce

Automating messaging consent in Salesforce is essential for maintaining compliance and scaling customer communication. In this guide, you’ll learn how to auto-create OPT-IN Messaging Consent records using a Record-Triggered Flow, eliminating manual work while ensuring accurate, real-time consent capture. If you’re searching for a reliable way to automate consent tracking in Salesforce without Apex and […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203239658
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada Canada
HIC Global Solutions INC
6D - 7398 Yonge St #1124 Thornhill, ON L4J 8J2 Canada +1(262) 310-7818