Site icon Salesforce News Technology Stuff

Change Data Capture using Asynchronous Apex Triggers

In Summer 19 release Salesforce introduce Create Change Data Capture and Asynchronous Apex Triggers. If you haven’t read Summer 19 release notes you can read them here. Once we save any record in Salesforce, it start a calculation known as transaction.

Sometimes these transaction takes longer time due to some complex calculation and we end up hitting governor limit. To avoid this we used future method but they have there own limitations and not always suitable. So Salesforce introduce Change Data capture and Asynchronous Apex Triggers.

Change Data Capture publishes the details of Salesforce data for new records or changed records. This feature can be enabled for any supported sObject in org. Once it is enabled, it starts publishing the events whenever we create, update, delete or undelete a record. The published event contains metadata information about the record and also all the changed field values.

So when a record is created or updated, Change Data Capture publishes an event and a change event trigger can then process that event asynchronously. Here is a simple example to understand it better. Once any record complete its execution then change data publish an event and then Asynchronous Apex Trigger starts processing.

First we need to enable Change Data Capture for object. In Setup Search for Change data Capture and open it.

Once we have enabled it we need to create trigger for this. There is two ways we can create the trigger.

  1. First from Developer Console > New > ApexTrigger and select the ObjectChangeEvent in object dropdown.
  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) {
	system.debug(Trigger.New);
}

Above is a sample code and now we can add our complex logic which we can process Asynchronously.

Test Change Event Triggers

To test change event triggers, We first need to enable the generation of change event notifications for the test method.

Test.enableChangeDataCapture();

The Test.enableChangeDataCapture() method ensures that Apex tests can fire change event triggers regardless of the entities selected in Setup. This method doesn’t affect the Change Data Capture entity selections for the org.

After performing DML operations, deliver the event messages to the corresponding trigger with:

Test.getEventBus().deliver();

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

To enable debug logs for Change Data Capture or Asynchronous Apex Triggers we need to setup Entity Type as Automated Process then only we can view the debug log. Or in Developer console deselect Show My Current Log Only checkbox.

So we have studied what is Change Data Capture and asynchronous Apex trigger. How we can use them. How we can write test class for them and how we can debug them.

Do you have any questions or want to add anything, let me know in comments. Happy Programming 🙂

Exit mobile version