Sub topic 12
Subscribing to Change Data Capture events
This chapter assumes you can already open a Subscribe stream, decode Avro payloads with a cached schema, and track replay IDs. It covers what changes when the topic carries Change Data Capture (CDC) events instead of platform events.
What CDC publishes
CDC publishes a change event whenever a record in a selected object is created, updated, deleted, or undeleted. You select objects in Setup under Change Data Capture. Once an object is selected, its change events become available as topics.
| Topic | Delivers |
|---|---|
| `/data/AccountChangeEvent` | Changes to Account records |
| `/data/Invoice__ChangeEvent` | Changes to the custom object `Invoice__c` |
| `/data/ChangeEvents` | Changes to every selected object |
You subscribe to all of these with the same Subscribe code from the previous chapters. Only the topic name differs.
Reading the change event header
Every change event has a ChangeEventHeader field. Read it before the field values, because it tells you what happened and to which records.
entityName: the object that changed, such asAccount.changeType:CREATE,UPDATE,DELETE, orUNDELETE. It can also be a gap or overflow type, described below.recordIds: the IDs of the affected records. One event can cover several records of the same type that changed in the same transaction.changedFields: the fields that changed on an update.commitTimestampandtransactionKey: when and in which transaction the change was committed.
def handle_change_event(record):
header = record["ChangeEventHeader"]
change_type = header["changeType"]
entity = header["entityName"]
if change_type.startswith("GAP_"):
# Salesforce could not build the full event. Re-read the records from the API.
for record_id in header["recordIds"]:
print("resync needed:", entity, record_id)
return
for record_id in header["recordIds"]:
print(change_type, entity, record_id)Call handle_change_event(record) in place of print(record) in the subscribe function from the Subscribe chapter.
Partial payloads
An UPDATE event contains only the fields that changed, plus the header. Fields that did not change are None in the decoded record. A None can also mean the field was set to null, so the header separates the two cases. changedFields lists the fields that changed, and nulledFields lists the ones set to null. In the Pub/Sub API these lists are bitmaps. You need to expand the bitmaps against the schema before you can read field names from them.
A CREATE event contains every populated field. A DELETE event contains the header and little else.
Gap and overflow events
Sometimes Salesforce cannot produce a normal change event. It sends a gap event instead, with a changeType such as GAP_UPDATE. A gap event carries the header but no field values. Overflow events (GAP_OVERFLOW) mean too many changes happened for one entity in a short time, so you have lost track of individual records. Treat both as a signal to fetch the current record state yourself.
Common mistakes
- Treating
Noneas "set to null". ChecknulledFieldsfirst. - Assuming one event means one record. Always loop over
recordIds. - Ignoring gap events. A mirror that skips them drifts out of sync with the org.
- Using
/data/ChangeEventsand assuming one schema. Events on this topic have different schemas, so rely on the per-eventschema_idand the cache.
Exercise
An UPDATE event on Account arrives with Phone set to None and Name set to None. Name is not in changedFields, and Phone is in nulledFields. What happened to each field?
Show answer
Phone was set to null in the update. Name did not change, so the event omits its value and decodes as None. Only the header lists show the difference between the two cases.