10

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.

5
  • 2
    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
    Commented Aug 2, 2019 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, Commented Aug 5, 2019 at 23:03
  • After about 1 year later, are you using the Async Trigger in your managed package?
    – Jair
    Commented May 15, 2020 at 6:28
  • @Jair No. Not currently. It wasn't worth the extra complexity. I went with other async processes that didn't have complex deployment requirements. Commented May 17, 2020 at 19:26
  • Thanks for your information.
    – Jair
    Commented May 18, 2020 at 2:06

1 Answer 1

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.

3
  • 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. Commented Aug 5, 2019 at 19:28
  • 1
    I see this is marked as answer, but in my org I've deployed PlatformEventChannelMember items and they are showing up in Change Data Capture screen as well but none of the items are showing up when I want to add them in my Managed Package in the same org. Were you able to add them and deploy them as part of your managed package as well?
    – VarunC
    Commented May 31, 2022 at 23:25
  • I was lost in the lack of documentation, saved me hours of trial and error, PlatformEventChannelMember did the trickThanks!
    – Bruno Diaz
    Commented Aug 6, 2023 at 12:35

You must log in to answer this question.

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