2

I've been following/researching a few resources online to create a apex trigger that gets triggered after actions on Account objects.

I'm trying to test the first bit so far where I want to capture the entityName, changedFields and changeType of the saved changes of an account. I will be publishing them to an endpoint after but right now I'm trying to get the logic on capturing the data correct.

trigger MyTrigger on Account (after insert, after update, after delete, after undelete) {

    if (Trigger.isInsert) {
        System.Debug('Trigger has been called after Insert.');
        for (Account event : Trigger.new){
            EventBus.ChangeEventHeader header = event.ChangeEventHeader;   // <---- issue here
            String changeEntity = header.entityName;
            List<String> changedFields = header.changedfields;
            String changeOperation = header.changeType;
        }
    }
    else if (Trigger.isUpdate) {
        System.Debug('Trigger has been called after Update.');
    }
    else if (Trigger.isDelete) {
        System.Debug('Trigger has been called after Delete.');
    }
    else if (Trigger.isUndelete) {
        System.Debug('Trigger has been called after UnDelete.');
    }
}

The developer console states "Variable does not exist: ChangeEventHeader". Thing is I was following a resource on the salesforce website. So this isn't really making sense why I cannot call ChangeEventHeader that way.

So how do I capture the wanted fields after insert/update etc.. with the apex trigger?

1 Answer 1

4

You will want to use the AccountChangeEvent object instead of Account in your trigger header.

trigger MyTrigger on AccountChangeEvent (after insert)
2
  • I don't have an option for AccountChange only AccountChangeEvent. Is this the same but updated version? Feb 11, 2022 at 15:54
  • @pragmaticlearner yeah, I'm on my phone, I probably messed up, lol. But the point is that the Account trigger is incorrect.
    – sfdcfox
    Feb 11, 2022 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .