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.
  1. In the Developer Console, select File | New | Apex Trigger.
  2. In the Name field, enter a name for the trigger: MyAccountChangeTrigger.
  3. From the dropdown, select the change event object for Account: AccountChangeEvent.
    The trigger is created with the after insert keyword.
  4. Replace the default content of the trigger with the following code.
    1trigger MyAccountChangeTrigger on AccountChangeEvent (after insert) {
    2    List<Task> tasks = new List<Task>();
    3    
    4    // Iterate through each event message.
    5    for (AccountChangeEvent event : Trigger.New) {
    6        // Get some event header fields
    7        EventBus.ChangeEventHeader header = event.ChangeEventHeader;
    8        System.debug('Received change event for ' + header.entityName +
    9                    ' for the ' + header.changeType + ' operation.');         
    10
    11        // Get account record fields
    12        System.debug('Account Name: ' + event.Name);
    13        System.debug('Account Phone: ' + event.Phone);
    14        
    15        // Create a followup task
    16        if (header.changetype == 'CREATE') {
    17            Task tk = new Task();
    18            tk.Subject = 'Follow up on new account for record or group of records: ' +
    19              header.recordIds;
    20            // Explicitly set the task owner ID to a valid user ID so that
    21            // it is not Automated Process.
    22            // For simplicity, we set it to the CommitUser header field, 
    23            // which is available for all operations. 
    24            tk.OwnerId = header.CommitUser; 
    25            tasks.add(tk);
    26        }        
    27        else if ((header.changetype == 'UPDATE')) {
    28            // For update operations, iterate over the list of changed fields
    29            System.debug('Iterate over the list of changed fields.'); 
    30            for (String field : header.changedFields) {
    31                if (null == event.get(field)) {
    32                    System.debug('Deleted field value (set to null): ' + field); 
    33                } else {
    34                    System.debug('Changed field value: ' + field + 
    35                        '. New Value: ' + event.get(field)); 
    36                }
    37            }
    38        }     
    39    }
    40    
    41    // Insert all tasks in bulk.
    42    if (tasks.size() > 0) {
    43        insert tasks;
    44    }
    45
    46}
    This simple trigger writes header and field values to the debug log for each received change event message. The trigger uses the changedFields header field to determine which fields changed in an update operation. The trigger also creates a follow-up task for new accounts.

    The changedFields property is available in Apex saved using API version 47.0 or later.

    Note

  5. To test the trigger, create an account with a name and phone.
  6. Edit the account, change the name, delete the phone value, and save the record.
  7. In Setup, enter Debug Logs in the Quick Find box, then select Debug Logs.
  8. 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.
    1...|DEBUG|Received change event for Account for the CREATE operation.
    2...|DEBUG|Account Name: Quick Start Account
    3...|DEBUG|Account Phone: 4155551212
  9. 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’s new value is listed. The phone field’s value was deleted, and its value was set to null. Also, because the system updates the LastModifiedDate field when the record is updated, this field is listed in the changedFields field and is part of the debug output.
    1...|DEBUG|Received change event for Account for the UPDATE operation.
    2...|DEBUG|Account Name: Quick Start Account Updated
    3...|DEBUG|Account Phone: null
    4...|DEBUG|Iterate over the list of changed fields.
    5...|DEBUG|Changed field value: Name. New Value: Quick Start Account Updated
    6...|DEBUG|Deleted field value (set to null): Phone
    7...|DEBUG|Changed field value: LastModifiedDate. New Value: 2019-08-14 23:20:15