Sub topic 5
Anatomy of a change event
This chapter assumes you know what a change event channel is and how to find its name, such as /data/AccountChangeEvent. It also assumes you can read JSON and basic Apex. Here you will learn what a delivered message contains and how to read it correctly.
The two layers of a message
A delivered change event has two layers.
- The envelope is added by the event bus. It carries the
replayId, which a subscriber uses to resume after a disconnect. How the envelope looks depends on the client. A CometD client such as empApi receivesmessage.data.event.replayId. An Apex trigger readsevent.ReplayId. - The payload is the change event itself. It has a
ChangeEventHeaderobject, followed by field values from the changed record.
The header describes the change. The field values hold the data. Most integration bugs come from treating these two parts as one.
A sample payload
This is a simplified update event for an Account. The ID, timestamp, and API version values are placeholders.
{
"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"
}Only Phone was edited, yet LastModifiedDate appears too. The platform updates that system field itself, and it counts as a change. Do not assume the changed fields are only the ones a user touched.
ChangeEventHeader fields
| Field | Meaning |
|---|---|
| `entityName` | API name of the object, such as `Account` or `Invoice__c`. |
| `recordIds` | List of IDs of the records affected by this event. |
| `changeType` | `CREATE`, `UPDATE`, `DELETE`, or `UNDELETE`. Gap and overflow events use `GAP_*` and `OVERFLOW` values. |
| `changedFields` | Names of the fields whose values changed. |
| `nulledFields` | Names of fields that were set to null. |
| `diffFields` | For long text fields, a compact description of the edit instead of the full value. |
| `changeOrigin` | The client or API that made the change, including its API version. |
| `transactionKey` | Identifies the transaction that made the change. |
| `sequenceNumber` | Position of this event within its transaction. |
| `commitTimestamp` | When the transaction committed, in milliseconds since the Unix epoch. |
| `commitUser` | ID of the user who made the change. |
| `commitNumber` | A number that increases with each commit, used to order changes. |
recordIds is a list
One event can describe several records. When a single transaction makes the same kind of change to records of the same object, for example a bulk update that sets the same fields, the platform may group them into one event. Always loop over recordIds. Code that reads only the first ID silently drops records. The sample trigger later in this chapter loops for this reason.
Ordering and grouping
Two header fields help you reason about order.
transactionKeyplussequenceNumberlet you put events from one transaction in order and group them together. If one save changes an Account and its Contacts, the events share a key.commitNumberandcommitTimestamporder events from different transactions. PrefercommitNumberfor ordering. Timestamps can tie, and the number can't.
Origin and user
changeOrigin tells you which client made the change. A common use is loop prevention. If your integration writes back to Salesforce, its own writes produce change events. Compare changeOrigin or commitUser against your integration's identity and ignore events it caused. Otherwise two systems can update each other endlessly.
Field values depend on the change type
The header is always present. The field values vary.
| `changeType` | Field values in the payload |
|---|---|
| `CREATE` | All fields that were populated when the record was created. Fields left empty are not included. |
| `UPDATE` | Only the fields that changed, plus the header. |
| `DELETE` | The header only. Use `recordIds` to find the records. |
| `UNDELETE` | All populated fields, as with a create. |
An update event is a partial record. If your external system needs the whole record, it can't rebuild it from the event alone. It must either keep its own copy and apply the changes, or query Salesforce for the rest. The next chapter covers each change type in more detail.
changedFields versus nulledFields
A field set to null does not appear as "Phone": null in the body. It is listed in nulledFields instead. This matters because missing from the body could mean either "unchanged" or "cleared".
The rule for an UPDATE event is:
- If the field is in
changedFieldsand has a value in the body, it changed to that value. - If the field is in
nulledFields, it was cleared. - If it is in neither, it did not change.
An update that clears the phone number therefore looks like this:
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "UPDATE",
"changedFields": ["LastModifiedDate"],
"nulledFields": ["Phone"]
},
"LastModifiedDate": "2026-12-01T09:05:00.000Z"
}This payload is trimmed to the relevant header fields. A real one carries all of them.
Compound fields
Some fields are compound, meaning one logical field is stored as several components. An address is made of street, city, state, postal code, and country. A person name is made of first and last name. In a change event, the components are delivered as separate fields, and changedFields lists the components. Subscribers expecting a single BillingAddress value will not find it where they expect it. Check for the component field names, for example BillingCity.
Reading the header in code
A subscriber usually does two things with the header. It decides whether the event is relevant, then it extracts the values. This plain JavaScript function works with either an empApi message payload or a parsed JSON payload. You can run it with Node.js.
function summarizeChange(payload, watchedField) {
const header = payload.ChangeEventHeader;
const changed = new Set(header.changedFields || []);
const nulled = new Set(header.nulledFields || []);
let fieldState = 'unchanged';
if (nulled.has(watchedField)) {
fieldState = 'cleared';
} else if (changed.has(watchedField)) {
fieldState = 'set to ' + payload[watchedField];
}
return {
entity: header.entityName,
type: header.changeType,
records: header.recordIds,
field: watchedField,
fieldState: fieldState
};
}
const sample = {
ChangeEventHeader: {
entityName: 'Account',
recordIds: ['001XXXXXXXXXXXXXXX'],
changeType: 'UPDATE',
changedFields: ['Phone', 'LastModifiedDate'],
nulledFields: []
},
Phone: '555-0100',
LastModifiedDate: '2026-12-01T09:00:00.000Z'
};
console.log(summarizeChange(sample, 'Phone'));This prints an object whose fieldState is set to 555-0100.
Reading the header in Apex
In a change event trigger, the header is available as EventBus.ChangeEventHeader. The body fields are ordinary properties of the event, such as event.Phone. This trigger separates a changed phone number from a cleared one.
trigger AccountPhoneTrigger on AccountChangeEvent (after insert) {
List<Task> followUps = new List<Task>();
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
if (header.changeType != 'UPDATE') {
continue;
}
Boolean cleared = header.nulledFields.contains('Phone');
Boolean changed = header.changedFields.contains('Phone');
if (changed || cleared) {
for (String recordId : header.recordIds) {
followUps.add(new Task(
Subject = cleared ? 'Phone removed, find a new number'
: 'Verify updated phone number',
WhatId = recordId
));
}
}
}
if (!followUps.isEmpty()) {
insert followUps;
}
}Common errors
- Reading only
recordIds[0]. Grouped events lose records. Loop over the whole list. - Treating a missing field as unchanged without checking
nulledFields. Cleared values are lost. - Expecting a full record on UPDATE. You only get the changed fields.
- Expecting field values on DELETE. You get the header only.
- Ignoring
changeOrigin. Integrations that write back create feedback loops. - Using wall-clock timestamps for ordering. Use
commitNumber. - Looking for compound fields by their parent name. Use the component field names.
Exercise
A subscriber receives an UPDATE event with recordIds containing three IDs, changedFields of ["Rating", "LastModifiedDate"], nulledFields of ["Phone"], and a body containing Rating and LastModifiedDate. Describe exactly what happened to each of the three records, and say why the body has no Phone entry.
Show answer
All three records were updated in one transaction, and each had the same change applied. Their Rating was set to the value in the body, their Phone was cleared to null, and the platform updated LastModifiedDate. Phone is not in the body because null values are not delivered as fields. They are listed in nulledFields instead. Any other field is unchanged, and the subscriber must loop over all three IDs.
Exercise
Your integration updates Accounts in Salesforce and also subscribes to Account change events. After you deploy it, the same records keep being updated back and forth. Which header fields can you use to stop the loop?
Show answer
Use changeOrigin to recognize changes made through your integration's client, or commitUser to recognize changes made by the integration user. Skip events that match, so your own writes are not processed again.