Sub topic 15
Integration design patterns and common pitfalls
This chapter assumes you know how to enable Change Data Capture (CDC) for an object, read a ChangeEventHeader, write an Apex trigger on a change event, and subscribe with a replay ID. It also assumes you know what gap and overflow events are. Here you combine those pieces into designs that hold up in production.
Pattern 1: Keep an external store in sync
A common goal is a mirror of Salesforce data in an external database. The subscriber receives each change event and applies it to the mirror.
Follow these rules:
- Store the last processed
replayIdonly after the external write succeeds. If the subscriber crashes, it resumes from the stored value and no event is lost. - Make each write idempotent. Replay can deliver an event again, so applying it twice must give the same result. Upserting by record ID satisfies this.
- Use
changedFieldsto update only the columns that changed. An update event carries only changed fields, so do not overwrite other columns with nulls. - Handle all four change types. A delete event should remove or flag the mirrored row. An undelete event should restore it.
Pattern 2: Trigger follow-up work inside Salesforce
An Apex trigger on a change event suits work that should run asynchronously after the original transaction commits. Examples are creating a follow-up task or calling an external service. The trigger runs in its own transaction, so a failure there does not roll back the user's save.
Always bulkify. One trigger invocation can receive many events, and each event can list many record IDs. Collect records in a list and run one DML statement, as in the earlier AccountChangeTrigger.
Pattern 3: Update the UI live
A Lightning web component can subscribe through empApi and refresh when a record changes. Use replay ID -1 for new events only, because a UI has no use for old changes. Unsubscribe in disconnectedCallback so the component does not leak subscriptions.
Common pitfalls
| Pitfall | Why it hurts | Fix |
|---|---|---|
| Ignoring gap and overflow events | The mirror silently drifts from Salesforce | Query the affected records, or run a full reconcile, then continue |
| Assuming exactly-once delivery | Duplicates corrupt counters or create duplicate records | Make handlers idempotent, keyed by record ID and `replayId` |
| Treating the event as the full record | Update events hold only changed fields | Query the record, or keep a full copy in the external store |
| Depending on event order across records | Order is guaranteed only per the replay sequence, not by business meaning | Compare `commitTimestamp` or `sequenceNumber` before overwriting |
| Waiting too long to resume | Events expire after the retention window | Alert on subscriber downtime and plan a full resync |
| Changing data in a trigger that fires the same event | One change can cause a feedback loop | Check `changeOrigin` or `commitUser` and skip your own integration's writes |
Design checklist
- Decide whether near-real-time is needed. If a nightly batch is enough, CDC adds complexity you do not need.
- Estimate event volume against your allocations before launch.
- Confirm the subscribing user has access to the objects and fields. Events are filtered by that user's permissions.
- Store the replay ID durably and test the resume path.
- Write a reconciliation job for gap, overflow, and long outages.
Exercise
Your subscriber receives a gap event for the Account channel. What should it do, and why?
Show answer
Treat the data for the affected records as unknown. Query Salesforce for the current state of those records, or run a wider reconcile if the gap does not identify them, and update the external store. Then continue processing events. Updates after the gap may carry only partial fields, so applying them without reconciling would leave the mirror wrong.
Exercise
An integration user updates Accounts through the API. Your trigger on AccountChangeEvent writes back to Account, which creates more change events. How can you stop the loop?
Show answer
Read changeOrigin or commitUser from the ChangeEventHeader and skip events that came from the integration's own user or API origin. You can also update only when a field value differs from the new value.