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.
Note - To capture the debug logs for this trigger, you need to set debug logs under "Automated Process". Otherwise logs will not show anywhere.