Sub topic 2
Anatomy of a Change Event
Assume you know Salesforce objects, Apex triggers, DML, JSON, and the publish/subscribe model. A Change Data Capture (CDC) event is a message that describes a record change. Salesforce publishes it on the event bus, where Apex triggers, Lightning components, or external clients can receive it.
Event structure
Each enabled object has a change-event channel. For Account, the channel is /data/AccountChangeEvent. The event contains a ChangeEventHeader and payload fields.
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "UPDATE",
"changedFields": ["Phone"],
"transactionKey": "example-transaction-key",
"sequenceNumber": 1,
"commitTimestamp": 1700000000000
},
"Phone": "555-0100"
}The header identifies the object, affected records, change type, transaction, and commit position. An update normally includes the fields that changed. A create includes the created record's fields. Delete and undelete events mainly provide record identity and change metadata.
| Header value | Purpose |
|---|---|
| `entityName` | Object that changed |
| `recordIds` | IDs of affected records |
| `changeType` | `CREATE`, `UPDATE`, `DELETE`, or `UNDELETE` |
| `changedFields` | Fields changed by an update |
Reacting with Apex
An Apex change-event trigger runs after Salesforce publishes events. Check the header before creating work, and bulkify DML.
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;
}
}Subscribing from clients
A Lightning web component can subscribe with lightning/empApi. Replay ID -1 requests only new events.
import { LightningElement } from 'lwc';
import { subscribe, unsubscribe } from 'lightning/empApi';
export default class AccountChangeListener extends LightningElement {
subscription;
channelName = '/data/AccountChangeEvent';
connectedCallback() {
subscribe(this.channelName, -1, (message) => {
const header = message.data.payload.ChangeEventHeader;
console.log(header.changeType, header.recordIds);
}).then((response) => {
this.subscription = response;
});
}
disconnectedCallback() {
if (this.subscription) {
unsubscribe(this.subscription, () => {});
}
}
}External consumers use CometD or the Pub/Sub API. Store received replay IDs durably so a restarted subscriber can resume. If the requested event is outside the retention window, reconcile records instead of assuming the stream is complete.
Exercise
Which header property should an Apex trigger inspect to process only phone updates?
Show answer
Check changeType for UPDATE and check whether changedFields contains Phone.