6

I'm trying to move a function from the AfterInsert trigger on account, to a Change Data Capture event, AccountChangeEvent.

The problem is this function uses Trigger.OldMap to track if there were changes on a specific field.

Is there an equivalent in AccountChangeEvent? how can i get the old value of a field from ChangeEvent?

4

The Change Data Capture feature targets synchronization of changes to some persistent store. Because of this fact, each event only contains enough information to apply a specific change to a stored record.

As a result, Change Data Capture events contain neither old values prior to a change nor a complete snapshot of an sObject at any point in time besides its creation or undeletion.

The section Apex Change Event Messages in the Change Data Capture Developer Guide explains what data is actually available in each message:

Create For a new record, the event message contains all fields, whether populated or empty. […]

Update For an updated record, the event message contains field values only for changed fields. Unchanged fields are present and empty (null), even if they contain a value in the record. […]

Delete For a deleted record, all record fields in the event message are empty (null).

Undelete For an undeleted record, the event message contains all fields from the original record, including empty (null) fields and system fields.

The use of the after insert event is somewhat deceptive: the insert event in question is really the delivery of the change event, not the original record. By the time the change event is delivered, the old field values are gone in the database (the change was committed).

Since the expectation with the CDC feature is that you, the client, have some persistent store against which you're applying changes to synchronize, it's assumed that you already have the old values. This makes some trigger use cases difficult to adapt to the async model.

The "Async Apex Trigger" name, to me, is a bit overbroad. These triggers cannot replace or asynchronize many standard trigger use cases; their capabilities need to be carefully evaluated against a proposed use due to the specificity of the underlying feature set.

| improve this answer | |
  • Thanks for the thorough explanation! – Dana Griff Jul 1 '19 at 13:17
1

AccountChangeEvent trigger only supports after insert statement.

I think AccountChangeEvent object's trigger cannot be a replacement of Account Trigger, because AccountChangeEvent trigger fires after Account data has been committed (here, inserted) to database and change event message is published.

According to me, you could still use original after insert trigger an Account object to find what has been changed (using Trigger.old or Trigger.oldMap) and this AccountChangeEvent trigger can be used to perform post committed operations.

| improve this answer | |
  • By all means its not a replacement, i agree. But this is not my intention :) – Dana Griff Jul 1 '19 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.