Sub topic 6
Change types and what each event contains
This chapter assumes you can read a change event's ChangeEventHeader and have written a basic Apex trigger. It covers how the content of an event depends on its change type. Your handler code has to account for this.
The four change types
Every change event has a changeType value in its header. For a normal event it is one of four values:
| changeType | Fires when | Record fields in the body |
|---|---|---|
| `CREATE` | A record is inserted | All fields that have a value |
| `UPDATE` | A record is edited | Only the fields that changed |
| `DELETE` | A record moves to the Recycle Bin | None, only the header |
| `UNDELETE` | A record is restored | All fields that have a value |
The same event can describe more than one record. When a transaction applies identical changes to several records, recordIds holds all of their IDs. Always loop over recordIds instead of reading the first element.
Reading an update event
Update events are the ones that cause the most errors. The body includes only the fields that were modified, plus LastModifiedDate. Two header lists tell you more:
changedFieldsnames every field whose value changed.nulledFieldsnames fields that were set to null. A nulled field has no value in the body, so the header is the only place you can see the change.
Long text fields can appear in diffFields instead. Its entries describe the difference rather than the full value.
Never assume a field is present in an update event. Check the header first:
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
Boolean phoneCleared = header.nulledFields.contains('Phone');
Boolean phoneChanged = header.changedFields.contains('Phone');If you read event.Phone on an update that did not touch the phone, you get null. That is indistinguishable from a cleared phone unless you check nulledFields.
Create and undelete events
A create event carries every field that has a value, so a subscriber can build the full record from the event alone. Fields with no value are left out. An undelete event also carries all populated fields, because the record returns to the org and a subscriber needs its current state.
Delete events
A delete event contains only the header. The recordIds value tells you which records to remove from your target system. You cannot read the old field values from the event. If your integration needs them, store them when you handle the earlier create and update events.
Branching on the change type
A trigger that handles every type uses a switch on changeType:
trigger AccountChangeRouter on AccountChangeEvent (after insert) {
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
switch on header.changeType {
when 'CREATE', 'UNDELETE' {
System.debug('Upsert ' + header.recordIds + ' using ' + event.Name);
}
when 'UPDATE' {
System.debug('Patch ' + header.recordIds + ': ' + header.changedFields);
}
when 'DELETE' {
System.debug('Remove ' + header.recordIds);
}
when else {
System.debug('Needs reconciliation: ' + header.changeType);
}
}
}
}The when else branch matters. Gap events use values such as GAP_CREATE, GAP_UPDATE, and GAP_OVERFLOW. They mean Salesforce could not produce a full event, and a later chapter covers them. Do not treat them as normal changes.
Exercise
A subscriber receives an UPDATE event for an Account. The body has no Phone field, but the header lists Phone in nulledFields. What happened to the record, and why can't you tell from the body alone?
Show answer
The Phone value was cleared (set to null) in that transaction. A nulled field has no value, so it does not appear in the body, and reading it returns null just like a field that was not changed. Only nulledFields shows that it was cleared.