top of page

Asynchronous Apex Triggers in Salesforce

Updated: Jan 17, 2020

In summer 2019 release, salesforce has introduced Asynchronous Apex triggers Aka Change Event Triggers.

With these change event trigger you can now work on change event messages. These triggers run asynchronously after the database transaction is completed. To avoid CPU time out error these trigger are perfect. User can now keep transaction based logic inside object trigger (normal triggers), and put the resource intensive processes inside change event trigger.

Change Event Trigger are the after insert Trigger, and defined as follows.

This is change event trigger for Account object. To define change event trigger for any custom object name of trigger will be changed as follows.

So the trigger name for custom object will be

"Name of Custom Object"__ChangeEvent.

How to work on Change Event Trigger :-

1. First of all, event for particular object needs to be enabled. For this Goto Setup -> Search "Change Data Capture", and select a desired object to "Selected Entities".

When you enable this setting, whenever any insert, update, delete or undelete operation happen on that particular object, it will fire the change event, which will be caught by your change event trigger automatically.

2. Write a trigger. Now you can define you change event trigger as above and process the change event.

Sample of Change Data Capture event published on update of an Opportunity record -

You can see that any field which has modified will have new values in it.

entity Name is the name of object

change type is the operation performed on this object

change origin - if the event published inside salesforce then it will contain salesforce in url. if it is outside then client will contain name of application that publish this event.

Let’s look at an example transaction. Whenever an opportunity record is created or updated, you need to check whether the corresponding account is qualified to be a high priority customer. This process of qualifying high priority customers is very limit-intensive and computationally slow.

Note - To capture the debug logs for this trigger, you need to set debug logs under "Automated Process". Otherwise logs will not show anywhere.
7,319 views1 comment

Recent Posts

See All

1 Comment

Deepak Thomas
Deepak Thomas
Oct 21, 2023

What is the difference between the ChangeEvent and @future methods? Why use ChangeEvent when we have @future to separate the thread from the main Trigger? The only difference I can think of is that CDC could be utilised as a PUB/SUB event.

bottom of page