0

My Apex code that does a Metadata.Operations.enqueueDeployment of some custom metadata records including in a 2GP managed package worked in a scratch org that had the namespace set, but fails when run as unpackaged code or when run from a second 2GP beta package that is using the same namespace with this error message:

Cannot create a new component with the namespace: xxx. Only components in the same namespace as the organization can be created through the API

I do have "Deploy Metadata from Non-Certified Package Versions via Apex" checked. And both packages are installed. Also the unit tests in the beta package pass. Note I am launching the failing code from the developer console i.e. running:

xxx.Migrator.migate();

Any suggestions for how to solve or what else to try?

1 Answer 1

0

The error message turned out to be spot on:

Only components in the same namespace as the organization can be created through the API

Multiple metadata types are involved in my code and some convoluted migration logic. In one case the code was trying to insert a custom metadata record using a name that still had the package namespace prefix on it. The development was done in a scratch org that had the namespace set so the error did not happen there. But when the code was run in orgs where the package was deployed i.e. organizations that had no namespace defined the error did happen.

So it looks like this Apex API explicity doesn't allow namespaced custom metadata records to be inserted, whether the code is running from the Developer Console or from within a package of that namespace. And that is fine - just make sure there are no namesapce prefixes in your data.

You must log in to answer this question.

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