4

First, some quick background. I'm aware there are several different ways this could be solved. Platform events, Queueables, batch jobs, etc... For now I'm specifically interested in how this would be done using Change Data Capture events.


I have a custom object that is part of a managed package. Whenever there is a change to a specific field on records from this custom object I need to run an expensive process to bring some of the data into sync with an external system. It will likely involve several callouts given the capabilities of the external API.

As such, the callouts can't be done immediately in the transaction where the update occurs. So they should be processed asynchronously after the record is changed, but this should be reasonably timely.


I'm experimenting with using an Apex trigger on the Change Data Capture event of the custom object. I have the change capture trigger setup to detect changes to the field of interest and build up a set of record IDs that need further processing. This all gets passed off to an asynchronous method that can make the required callouts etc...

The problem I'm having is how to include this Change Data Capture trigger in a managed package and make if fire after installing the package.

When I create a new package version I'm forced to include the change event trigger. That's odd, as nothing in the package explicitly references the __ChangeEvent after insert trigger. But I want it in the package anyway, so whatever.

That deploys fine along with the associated test cases that use Test.enableChangeDataCapture(); and Test.getEventBus().deliver();.

What isn't getting packaged are the CdcObjectEnablement records that are configured under "Change Data Capture" in setup (See Select Objects for Change Notifications). Without the custom object being included in the "Selected Entities" the change event trigger won't fire. I'd have to manually activate this set in each subscriber org where I want to change event to fire.

enter image description here

I can't figure out where this configuration is stored in the metadata or which sObject is used to represent it. I have my suspicions about PlatformEventChannelMember (0v8), but I can't access it.

  • 1
    Also I tested which also uses customer org's limitation 5 objects that does not easily to add an Async trigger into a managed package. – Jair Aug 2 at 15:58
  • 1
    @Jair Yes, working within the default selected entity allocations is something that will need careful consideration for a managed package. There is a good chance that a subscriber org might not have capacity to add another PlatformEventChannel, – Daniel Ballinger Aug 5 at 23:03
1

Have not tried this, but the PlatformEventChannel metadata type appears to be the documented way to do this, see https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_object_selections_metadata.htm.

  • That looks like the correct metadata for the CDC selected events. Currently it looks like it is only exposed via the metadata API. There isn't anything in the Tooling API for it. – Daniel Ballinger Aug 5 at 19:28

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.