Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles


Change Data Capture and Asynchronous Apex Trigger

By |2021-01-28T03:40:59+00:00January 28th, 2021|

When we insert and do changes on SObject records and save them on Salesforce, platform start a series of calculations called apex transaction. Sometime due to complex calculations, it takes a lot of time to complete a transaction which in results CPU time out exception. You can reduce these transaction time and limit constraints by decoupling resource-intensive, non-transactional logic from database and run asynchronously.

This can be achieved by this Salesforce feature change data capture and a new enhancement in Summer’19 release, asynchronous apex trigger.

What is Change Data Capture and Asynchronous Apex Trigger?

Change Data Capture is a streaming product on lightning platform that enables you to integrate your Salesforce data to external system. This feature enables you to replicate data changes to external systems. CDC publishes the events which stores deltas of Salesforce data for new and changed records. This feature can be enabled for some standard objects and all custom objects. Once it is enabled, it starts publishes the events whenever record create, update, delete or undelete. The published event contains metadata information of record and changed field values.

Asynchronous trigger is a change event trigger which run asynchronously after a database transaction is completed. The async execution of change event trigger makes them ideal for processing resource-intensive business logic in separate transaction from database transaction.

How does Change Data Capture and Asynchronous Apex Trigger?

Whenever a record is created, updated, deleted or undeleted then CDC will publish events. And trigger will process those events. These triggers are after-insert trigger and run on change event objects instead of SObjects.

Let me explain it by an example.

By creating trigger on Opportunity Change Event object named as Opportunity Change Event Trigger. You can create trigger by using CLI form VS code terminal by this command:

sfdx force: apex: trigger:create -n Opportunity Change Event Trigger -s Opportunity Change Event -e ‘after insert’ -d ‘force-app/main/default/triggers’.

From the Developer Console by selecting File→New→ Apex Trigger and then OpportunityChangeEvent for the sObject.

Whenever opportunity record is created, updated, deleted or undeleted, change data capture will fire events which will run this trigger before that we must enable CDC feature on opportunity object. So what trigger is doing whenever saved record field isWon is true, creating a follow up task. Below are code snippets.

Change Data Capture and Asynchronous Apex Trigger

Change Data Capture and Asynchronous Apex Trigger


  • Async triggers are ‘after-insert’ triggers run only when changes are committed to database.
  • Multiple async trigger run works on same objects. Each have their own limits.
  • These triggers run asynchronously but have synchronous limits like no extra heap size, CPU time and SOQL query limit.
  • Like object trigger, these are also not able to make call to external API.
  • contains max of 2000 published events. So, at time it can process 2000 events.
  • Set Automation Process as trace entity for debug logs of async triggers.
  • There is no limit concept for CDC.
  • Maximum 5 objects can be enabled for CDC feature for dev orgs, it can be extended by purchasing CDC license.
  • CDC is supported for some of standard objects and all custom objects.

Use Cases:

  • For Proper decoupling and clean architecture, async trigger is better option than other async processes.
  • For every scenario where you need to break the transaction, like you want to separate database processes and business calculations.





Leave A Comment