Loading...
New Winner – Cross-Border Excellence | Payoneer India 2026 Check out
Featured Recognized by Salesforce; Proven by impact! Check out

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
How to Connect Salesforce with Claude AI Using an MCP Server

Claude AI can make working with Salesforce CRM data much easier. Instead of manually opening records, building reports, or writing SOQL queries, teams can ask Claude questions in plain English and get answers from their Salesforce org. With Salesforce MCP, users can explore CRM information through a conversational interface, helping them find relevant details with […]

Read More
How to Set Up Salesforce Claudeforce Using the Headless 360 MCP Server

Setting up Salesforce Claudeforce requires more than simply connecting Salesforce with Claude. The process involves beta access, the Salesforce Headless 360 MCP Server, an External Client App, plugin configuration, and user-level permissions. Each part of the setup is important because Claude needs controlled access to Salesforce data and actions while still following the user’s existing […]

Read More
How to Extract Data from PDFs Using OCR.space API in Salesforce

Many times it’s crucial to extract information from documents such as loan statements, invoices, applications, identity documents, and scanned forms. When the information is stored as an image or scanned PDF, it cannot be directly read and used as regular text. This is where Salesforce OCR becomes useful. Optical Character Recognition can convert text from […]

Read More
Passwordless Login with Passkeys in Salesforce: A Practical Setup Guide

Passwordless login with passkeys gives Salesforce users another way to authenticate without entering a traditional password every time. Instead, users can verify their identity through a device, browser, or supported passkey manager. For Salesforce admins and consultants, setting up passkeys requires a little planning. Each user needs access to their own registered passkey, and backup […]

Read More
How to Configure Salesforce for Google Chat Integration

Google Chat integration with Salesforce connects team communication with Salesforce data, helping teams receive important updates without constantly switching between platforms. It makes collaboration simpler and keeps sales teams informed. The integration works across three key components: Salesforce: Acts as the source system for business information, record updates, and automation. Google Cloud Console: Provides the […]

Read More
How to Integrate Tasks Between NikoHealth and Salesforce

How to Integrate Tasks Between NikoHealth and Salesforce Tasks play an important role in healthcare workflows and can be associated with Patients, Orders, Invoices, Documents, and Assigned Staff Members. With MultiSync Made Easy, organizations can synchronize Task records while maintaining these important relationships. This Niko Health image-based workflow also makes it easier to understand how […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203669505
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
CA Flag Canada
HIC Global Solutions INC
6D - 7398 Yonge St #1124 Thornhill, ON L4J 8J2 Canada +1(262) 310-7818