Sub topic 6
Change types and what each event contains
This chapter assumes you can read a change event's ChangeEventHeader, know which channel an object publishes on, and have written a basic Apex trigger. It covers the four record change types and the gap types, and what each carries in its payload. A subscriber that treats every event as a full copy of the record will break on the first update.
The change type is in the header
Every change event has a changeType value in its ChangeEventHeader. That value tells you how to read the rest of the message. There are nine possible values:
CREATE,UPDATE,DELETE,UNDELETE: a record changed.GAP_CREATE,GAP_UPDATE,GAP_DELETE,GAP_UNDELETE: Salesforce could not generate the full event for one or more records.GAP_OVERFLOW: a large number of changes overwhelmed event generation for an entity.
The first four are the normal cases and are the focus here. Gap types are introduced at the end and covered fully in a later chapter.
What each type contains
The header is always present. The difference is in the fields that follow it.
| Change type | Triggered by | Fields after the header |
|---|---|---|
| `CREATE` | A record is inserted | Every field that has a value, including system fields such as `CreatedDate` and `OwnerId` |
| `UPDATE` | A record is edited | Only the fields that changed, plus `LastModifiedDate` and related system fields |
| `DELETE` | A record moves to the Recycle Bin | None. The header identifies the record and nothing else |
| `UNDELETE` | A record is restored from the Recycle Bin | Every field that has a value, like `CREATE` |
The pattern is that create and undelete give you a snapshot, update gives you a delta, and delete gives you only an identity.
The record lifecycle
The diagram shows how the four types map onto what happens to a record. Only an existing record can be updated or deleted, and only a deleted record can be undeleted.
CREATE: a full snapshot
A CREATE event is the only time you receive the record's initial state in one message. Anything the user left blank has no value, so the field is empty or absent depending on the client. Handle both cases.
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "CREATE",
"changeOrigin": "com/salesforce/api/rest/60.0",
"transactionKey": "example-transaction-key",
"sequenceNumber": 1,
"commitTimestamp": 1700000000000,
"commitUser": "005XXXXXXXXXXXXXXX",
"commitNumber": 1700000000000000
},
"Name": "Acme",
"OwnerId": "005XXXXXXXXXXXXXXX",
"CreatedDate": "2026-12-01T09:00:00.000Z",
"LastModifiedDate": "2026-12-01T09:00:00.000Z"
}A create event has no changedFields list to inspect, because everything is new. If your system keeps a copy of the record, a CREATE event is the signal to insert a row.
UPDATE: a delta, not a copy
An UPDATE event contains only what changed. If a user edits Phone on an account with 40 populated fields, you receive Phone, the updated LastModifiedDate, and the header. You do not receive Name, Industry, or any other unchanged field.
Three header lists describe the delta:
changedFieldslists the fields whose values changed.nulledFieldslists fields that were set to null.diffFieldslists long text fields for which the event carries a diff of the change, not the whole value.
The consequence is that a subscriber cannot rebuild the current record from one update event. It has to apply the delta to a copy it already holds, or query Salesforce for the full record. The first approach is cheaper but depends on never missing an event. The second approach costs an API call per event but self-corrects.
Compound fields
Compound fields such as BillingAddress are published through their component fields. If only BillingCity changes, expect BillingCity in the payload and BillingCity in changedFields. Code that looks for BillingAddress will not find it. Check the component fields you need.
Multiple records in one event
recordIds is a list for a reason. When several records in one transaction receive the same kind of change with the same values, Salesforce can bundle them into a single event. A bulk update that sets the phone number on 50 accounts to the same value can arrive as one event with 50 IDs.
Code that reads only recordIds[0] silently drops the other 49. The trigger below handles the list correctly.
DELETE: identity only
A DELETE event has the header and no field values. The header gives you entityName, recordIds, and the commit details, and that is all. If you need to know what the record contained, for example to find a related external row by its email address, you must have stored that mapping before the delete. Keep the Salesforce record ID as the key in the external system so that a delete can be handled with the ID alone.
Deletion here means a move to the Recycle Bin. A record purged from the Recycle Bin does not generate a second event.
UNDELETE: a new snapshot
When a record is restored, Salesforce publishes UNDELETE with the full set of populated fields, as for CREATE. Most consumers should treat the two the same way: upsert the record by ID. A consumer that only inserts on CREATE and ignores UNDELETE will leave restored records missing from the target system.
Branching on change type in Apex
A trigger on AccountChangeEvent can use a switch statement on changeType. The trigger below logs each case and shows where each type's logic belongs. Gap types fall into the when else branch.
trigger AccountChangeRouter on AccountChangeEvent (after insert) {
for (AccountChangeEvent evt : Trigger.new) {
EventBus.ChangeEventHeader header = evt.ChangeEventHeader;
switch on header.changeType {
when 'CREATE', 'UNDELETE' {
// Full snapshot: upsert by record ID.
for (String recordId : header.recordIds) {
System.debug('Upsert ' + recordId + ' named ' + evt.Name);
}
}
when 'UPDATE' {
// Delta only: use changedFields to see what moved.
Boolean phoneCleared = header.nulledFields != null
&& header.nulledFields.contains('Phone');
Boolean phoneChanged = header.changedFields != null
&& header.changedFields.contains('Phone');
for (String recordId : header.recordIds) {
System.debug(recordId + ' phone changed: ' + phoneChanged
+ ', cleared: ' + phoneCleared);
}
}
when 'DELETE' {
// Header only: the ID is all you have.
for (String recordId : header.recordIds) {
System.debug('Remove ' + recordId);
}
}
when else {
// GAP_* and GAP_OVERFLOW: reconcile instead of trusting the event.
System.debug('Gap event: ' + header.changeType
+ ' for ' + header.entityName);
}
}
}
}The trigger runs asynchronously after the transaction that made the change has committed. It cannot roll that change back.
Reading the origin of a change
The header also tells you where a change came from, which matters when you build two-way integrations.
changeOriginnames the source, such as the REST API at a given version, or a client ID if the caller set one.commitUseris the ID of the user who made the change.transactionKeyandsequenceNumbergroup events from one transaction and order them within it.
An integration that writes back to Salesforce will see its own writes as change events. Filtering on commitUser or changeOrigin lets the integration skip those events. Without a filter, two systems can keep updating each other forever.
Gap events: when the event is not the change
Salesforce sometimes cannot produce a normal change event. The cause can be an internal error or a very large volume of changes. In that case it publishes a gap event instead. A gap event has a GAP_ change type, the affected recordIds, and no field values. It is a notice that a change happened and you must not rely on the event stream for that record.
GAP_OVERFLOW is broader. It means more than 100,000 changes occurred in a single transaction for an entity. Salesforce stops generating individual events and publishes one overflow event per entity. That event carries no record IDs.
The correct response to either kind is to refetch current state from Salesforce for the affected records, or for the whole entity after an overflow. The reconciliation approach is covered in the chapter on gap and overflow events.
Common mistakes
- Treating
UPDATEas a full record and overwriting an external row with mostly null values. - Handling only
CREATEandUPDATE, so deletes and restores never reach the target system. - Reading only the first entry of
recordIds. - Ignoring
nulledFields, so cleared values are never cleared downstream. - Skipping the
when elsebranch, so gap events pass without notice.
Exercise
A subscriber keeps a copy of Account records in an external database. It receives an UPDATE event whose changedFields is ["Industry", "LastModifiedDate"] and whose recordIds holds three IDs. What should it do, and why can it not simply replace each external row with the event body?
Show answer
It should apply Industry and LastModifiedDate from the event to all three external rows, one per ID in recordIds. The body is a delta and holds only the changed fields, so replacing a whole row would erase every other field. It should also check nulledFields for fields that were cleared.
Exercise
An external system receives a DELETE event for an Account. The event has no Name or Email. How can the system still find and remove the right row?
Show answer
The external system must key its rows by the Salesforce record ID, stored when the record was created. The DELETE header carries the ID in recordIds, so the system looks up each ID and removes the matching row. No other field values are available.
Exercise
Your trigger handles CREATE, UPDATE, and DELETE. Users report that restored accounts are missing from the target system. What is the likely cause and the fix?
Show answer
Restoring a record from the Recycle Bin publishes an UNDELETE event, which the trigger ignores. Add UNDELETE to the same branch as CREATE (for example, when 'CREATE', 'UNDELETE') and upsert the record by ID.