Newer Version Available

This content describes an older version of this product. View Latest

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            // Set the task owner ID to a user ID so it is not Automated Process.
    21            // For simplicity, we set it to the CommitUser header field, 
    22            // which is available for all operations. 
    23            tk.OwnerId = header.CommitUser; 
    24            tasks.add(tk);
    25        }        
    26        // For updates, check whether the Phone field was set to null explicitly
    27        // and is not null because it is unchanged.
    28        else if ((header.changetype== 'UPDATE') && (event.Phone == null)) {
    29            if (header.nulledFields.contains('Phone')) {
    30                System.debug('Phone field was set to null.');
    31            } else {
    32                System.debug('Phone field is unchanged.');
    33            }
    34        }       
    35	}
    36    
    37    // Insert all tasks in bulk.
    38    if (tasks.size() > 0) {
    39        insert tasks;
    40    }
    41
    42}
    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.
  5. To test the trigger, create an account with a name and phone.
  6. Edit the account, 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 is null because it is unchanged but the phone field was explicitly set to null.
    1...|DEBUG|Received change event for Account for the UPDATE operation.
    2...|DEBUG|Account Name: null
    3...|DEBUG|Account Phone: null
    4...|DEBUG|Phone field was set to null.