As part of a second-generation managed package, need to enable Change Data Capture (CDC) for the Opportunity object.
Can this be done via Apex in a PostInstall for the managed package? Been Googling for examples but could not find any.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityNo, it cannot.
Enabling Change Data Capture for an object is done via the Metadata or Tooling APIs against the PlatformEventChannelMember
entity.
Your postinstall scripts are subject to a variety of limitations, including that
It can’t access Session IDs.
You would need an API-enabled Session Id (for a System Administrator) to make a call to mutate PlatformEventChannelMember
.
This is something you'll generally have to handle through admin-oriented documentation unless you're delivering your package via a tool like (my team's) MetaDeploy, which can automate API-based setup steps after package installation.
You could also in theory handle it through an an-app setup UI that calls the Metadata API, but you'll have to do so carefully as Security Review standards for consuming session ids like that have changed recently, and I'm not myself fully up to speed on what's allowed.