[SalesForce] How to see debug logs for Change Data Capture triggers in Salesforce

With Summer '19 we can now have asynchronous apex change event triggers. This is covered in the release notes – Process Change Event Messages in Apex Triggers and the blog post Get #Buildspiration with Asynchronous Apex Triggers in Summer ‘19.

To test this I created a simple change event trigger on Opportunity to gather up the Opportunity Ids.

trigger OpportunityChangeEventAsyncTrigger on OpportunityChangeEvent (after insert) {
    Set<Id> oppIds = new Set<Id>();

    for(OpportunityChangeEvent oppChangeEvent : Trigger.new){
        List<Id> recordIds = oppChangeEvent.ChangeEventHeader.getRecordIds();
        oppIds.addAll(recordIds);
    }

    System.debug(oppIds);
}

I also ensured that the event would fire for Opportunity via Setup > Change Date Capture.
Enable Change Data Capture

Then I ran the following anonymous Apex in the Developer Console:

Opportunity testOpp = [Select Id,Description from Opportunity limit 1];
testOpp.Description += 'Change Data Capture';
update testOpp;

The Developer Console showed the logging for the anonymous apex, but nothing appeared for the change data capture event trigger.

How do I see debug logging for Change Data Capture event triggers?

Best Answer

As with How do I see debug logs for Platform Event triggers in Salesforce?, the trigger here is asynchronous and runs as the Automated Process entity rather than the user to initiated the transaction.

As such, there needs to be a TraceFlag created for Automated Process.

enter image description here

By default the Developer Console will only show debug logs for the current user, so be sure to uncheck "Show My Current Logs Only" under the debug menu.

enter image description here

That said, I've noticed an oddity with the Developer Console when the automated process TraceFlag is on. Occasionally it is directly opening the Change Capture event log rather than the transaction log when "Open Log" is checked.

Developer Console opening Platform Event Trigger debug log after anonymous apex

Related Topic