2

We are building a managed package which having an integration with external system. We are getting the external system credentials from the Subscriber org admin and storing it in a protected custom metadata. In one of our use case we would like to Delete this Custom MetaData record through our managed package apex controllers using the following code,

List<String> recordsToDelete = new List<String>();
recordsToDelete.add('SSCC__My_Custom_Type.record1');
service.deleteMetadata('CustomMetadata', recordsToDelete);

List<String> recordsToDelete = new List<String>();
recordsToDelete.add('SSCC__My_Custom_Type__mdt.record1');
service.deleteMetadata('CustomMetadata', recordsToDelete);

Both were working fine in package org. But throwing the below exception in package installed org,

It throws error, Status code - "INVALID_CROSS_REFERENCE_KEY" Error message - "In field: members - no CustomMetadata named My_Custom_Type.record1 found"

Am I missing anything here?

3
  • 1
    Try adding the managed package namespace prefix separated by a double underscore.
    – Keith C
    Mar 18, 2021 at 8:16
  • You edited the question to add the namespace prefix (first in one place then in the other). Please make sure the code sample is representative of what is really being done. Also note that a namespace prefix is followed by double underscore.
    – Phil W
    Mar 18, 2021 at 8:18
  • @PhilW, Keith: We appended the name space prefix with double underscore only. It's a typo early and I update the question now.
    – Rams
    Mar 18, 2021 at 8:48

1 Answer 1

1

Packaged custom metadata type instances that are part of a managed package cannot be deleted, exactly like any other packaged metadata. The best you can do is update the content of that instance using the Apex Metadata API. Alternatively, don't package the instance and instead generate it in the installer (though this probably means you can't make it "protected").

We explicitly have a Checkbox field, namespace__Active__c, in our CMT so we can disable consideration of the (protected or unprotected) CMT instance (we query the CMTs using SOQL and look for namespace__Active__c = TRUE as part of that query).

You can update other fields too, e.g. to clear the credentials you've stored.

I would ask if there is a reason not to use a Named Credential in this case?

You must log in to answer this question.

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