Change Data Capture & Asynchronous Apex Triggers

Sharing is caring!

In Salesforce, there are a wide range of features such as Queueable Apex, Batch job, Future method, and Scheduler Apex. In the Summer 2019 Release, Salesforce provided another innovative feature called “Asynchronous Apex Triggers” that has the ability to process data asynchronously with the help of Change Data Capture.

In this blog, we will study about what is Change Data Capture and Asynchronous Apex trigger. We will also find out ways to use them and how we can write a test class for them and debug them.

When To Use Change Data capture and Asynchronous Apex Triggers?

It is ideal if you need to communicate and/or replicate data changes from one system to another. This is primarily because data changes in one system should be notified to multiple other clients. Furthermore, business requirements can be addressed efficiently and effectively with near real-time process/data integration. 

Why To Use Change Data capture and Asynchronous Apex Triggers?

  • For sending data to an external system without code.
  • Optimizing efficient utilization of system resources. Events are published by source system and it is up to the subscribers to consume when required. Transaction independent activities such as email notifications can be performed asynchronously.
  • Decoupling integrated systems by efficiently utilizing the event publisher-subscriber model.

Why Change Data capture and Asynchronous Apex Triggers?

Once we save any record in Salesforce, a calculation gets started that is known as a transaction. Sometimes, these transactions take much longer than expectations because of some complex calculations and users end up hitting the governor limit. Salesforce introduced Change Data capture and Asynchronous Apex Triggers to avoid this complication.

Change Data Capture publishes the details of Salesforce data for changed or new records. This feature can be enabled by users for any supported sObject in org. Once it gets enabled, it starts publishing the events whenever users create, delete, update, or undelete a record. Metadata information about the record and the associated changed field values are included in the published event.

Change Data Capture publishes an event when a record is created or updated. The Change Event Trigger can then start processing that event asynchronously.

Let us understand this with the help of an example.

Once a record completes its execution, an event is published by the Change Data and then processing is initiated by the Asynchronous Apex Trigger.

What Is A Change Event Trigger?

A change event trigger can be described as an “after-insert” trigger that fires after the change event message is published. In the Developer Console, users can create an after-insert trigger the same way as an Apex object trigger gets created after enabling the object for Change Data Capture.

For enabling the objects for receiving change notification, users can follow the below step:

Setup-> Integrations -> Change Data Capture.

Differences Between Using Asynchronous Triggers And Using Queueable Or Future Apex In Triggers

Asynchronous Triggers Future/ Queueable Apex In Triggers
It gets executed only when a database transaction gets committed in the database. If the queue is empty, it can get executed in continuation even without the record commit. However, it will deliver an exception if the record fails in the parent trigger transaction.
Consistent and streamlined approach of handling complex transactions. It provides just a different way to handle complex transactions. It was the only way before the launch of Asynchronous Triggers.
Use context variables like
The use of context variables cannot be made.
For using it, we need to enable Change Data Capture for objects in the setup. It does not need object-specific enabling.
It doesn’t require an old or new map for finding out the value change in fields. Users have to face issues with creating lists, passing lists, checking trigger context variables, comparing values, etc.
It doesn’t have a higher set of governor limits. It has a higher set of governor limits.
It is the streamlined and native way to decouple complex logic. It is the old and redundant way to decouple complex business logic.

How To Create Asynchronous Apex Triggers?

In the Developer Console, you can create an after-insert trigger the same way as you create an Apex object trigger. You need to pick the change event object from the sObject dropdown that is populated with the change events available in the org when you create a trigger.

We need to create a trigger once we have enabled it. There are two ways by which the trigger can be created:

  1. Select the ObjectChangeEvent in object dropdown from Developer Console > New > ApexTrigger.
  2. Using CLI sfdx force:apex:trigger:create -n AccountAsyncTrigger -s AccountChangeEvent -e ‘after insert’ -d ‘force-app/main/default/triggers.

trigger AccountAsynChange on AccountChangeEvent (after insert) {



We can now add a complex logic that we can process asynchronously.

How To Test Change Event Triggers?

We need to enable the generation of change event notifications for the test method for testing change event triggers.


The Test.enableChangeDataCapture() method makes sure that Change event triggers can be fired by Apex tests irrespective of the entities selected in Setup. It is worthwhile to note here that the method does not have a negative impact on the entity selections of Change Data Capture for the org.

Once this is done, you can deliver the event messages to the corresponding trigger after performing Data Manipulation Language (DML) operations.


Alternatively, the associated trigger is fired by the change event messages after Test.stopTest() executes if we use the Test.startTest(), Test.stopTest() method block in our test.

Users need to setup Entity Type as Automated Process for enabling debug logs for Change Data Capture or Asynchronous Apex Triggers. They can then only view the debug log and may deselect Show My Current Log Only checkbox in the Developer Console.

If you need any assistance with Change Data Capture and Asynchronous Apex triggers to get the best value out of Salesforce Lightning Experience, trust Cloud Analogy – the leading name in the world of Salesforce Development Services and Salesforce Consulting Services.


Sachin Arora

Scrum Master and Principal Solutions Architect
Sachin, a renowned Scrum Master and Principal Solutions Architect at Cloud Analogy, has rich experience when it comes to working on process improvement in a fast-paced environment maintaining high level of quality in all deliverables. Sachin's expertise lies in varied hardware and software environments including Cloud technologies such as Salesforce, AWS, Cloud Foundry & Google App Engine and Mobile.

Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.

Leave a Reply

× How can I help you?