Newer Version Available
Add an Apex Trigger
The quick start adds a simple change event trigger that shows how to access header
and record fields in a change event message.
Before you add and test the trigger, set up debug logging for the Automated Process
entity and enable Account for Change Data Capture. See Prerequisites.
- In the Developer Console, select .
- In the Name field, enter a name for the trigger: MyAccountChangeTrigger.
-
From the dropdown, select the change event object for Account: AccountChangeEvent.
The trigger is created with the after insert keyword.
-
Replace the default content of the trigger with the following code.
This simple trigger writes header and field values to the debug log for each received change event message. The trigger uses the nulledFields header field to determine if the Phone record field was explicitly set to null in an update operation. The trigger also creates a follow-up task for new accounts.
- To test the trigger, create an account with a name and phone.
- Edit the account, delete the phone value, and save the record.
- In Setup, enter Debug Logs in the Quick Find box, then select Debug Logs.
-
To view the debug logs corresponding to the record creation, click the second
log in the list (logs are ordered by most recent first). The output of the
System.debug statements looks
similar to the following.
-
To view the debug logs corresponding to the record update, click the first log
in the list. The output of the System.debug statements looks similar to the following. The
account name is null because it is unchanged but the phone field was explicitly
set to null.