Sub topic 5
Anatomy of a change event
This chapter assumes you know the Salesforce data model, can read JSON, and understand publish/subscribe messaging. You have already enabled Change Data Capture (CDC) for an object and know its channel name. Here you learn what a message on that channel contains.
Two parts: header and fields
Every change event has two parts:
- A
ChangeEventHeaderobject that describes the change. - The record's field values, placed at the top level of the event next to the header.
The field values are not a full copy of the record. For an update, the event carries only the fields that changed, plus system fields such as LastModifiedDate. Create and undelete events carry all populated fields. The next chapter covers each change type in detail.
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "UPDATE",
"changedFields": ["Phone", "LastModifiedDate"],
"changeOrigin": "com/salesforce/api/rest/60.0",
"transactionKey": "example-transaction-key",
"sequenceNumber": 1,
"commitTimestamp": 1700000000000,
"commitUser": "005XXXXXXXXXXXXXXX",
"commitNumber": 1700000000000000
},
"Phone": "555-0100",
"LastModifiedDate": "2026-12-01T09:00:00.000Z"
}The IDs, timestamp, and API version here are placeholders. In a real event you will see actual values.
Header fields
| Field | What it tells you |
|---|---|
| `entityName` | The object that changed, such as `Account`. |
| `recordIds` | The IDs of the changed records. It is a list, so one event can cover several records. |
| `changeType` | `CREATE`, `UPDATE`, `DELETE`, or `UNDELETE`. Gap and overflow types also exist. |
| `changedFields` | The fields whose values changed. |
| `changeOrigin` | The source of the change, such as an API client and its version. |
| `transactionKey` | Identifies the transaction that made the change. |
| `sequenceNumber` | The order of this event within its transaction. |
| `commitTimestamp` | When the transaction committed, in milliseconds since the epoch. |
| `commitUser` | The ID of the user who made the change. |
| `commitNumber` | A number that orders committed transactions. |
Two header fields are easy to misread:
recordIdscan hold more than one ID. Salesforce can group changes of the same type made in the same transaction into one event. Always loop over the list.transactionKeyandsequenceNumbertogether let you rebuild the order of changes within one transaction.
Which fields appear in the body
The body follows these rules:
- Fields set to null on an update appear in
changedFieldsand in the body with a null value. Reading the body alone, you cannot tell "changed to null" from "not changed". UsechangedFieldsto decide. - Fields that did not change are absent on updates. Do not treat a missing field as null.
- Compound fields such as addresses are delivered as their component fields.
Reading the header in Apex
In a trigger, the header is available as EventBus.ChangeEventHeader. The trigger below runs on AccountChangeEvent, and it only acts on updates that touched Phone.
trigger AccountChangeTrigger on AccountChangeEvent (after insert) {
List<Task> followUps = new List<Task>();
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
if (header.changeType == 'UPDATE' && header.changedFields.contains('Phone')) {
for (String recordId : header.recordIds) {
followUps.add(new Task(
Subject = 'Verify updated phone number',
WhatId = recordId
));
}
}
}
if (!followUps.isEmpty()) {
insert followUps;
}
}The inner loop over recordIds handles grouped events. The single insert after the loops keeps DML to one statement.
Exercise
An update event arrives with changedFields set to ["Phone", "LastModifiedDate"] and a body with only those two values. Your code reads event.Name and gets null. Does the account have no name? What should you check instead?
Show answer
No. The event is an update, and unchanged fields are absent from the body, so a missing Name means it did not change. Check whether changedFields contains the field you care about before reading its value. If you need the full record, query it by the ID in recordIds.