Note: There is a related Salesforce Support Case #45059901
Convinced by the answers of some real ISV experts, I refactored my Managed Release Package from ugly Custom Settings to Custom Metadata with packaged records.
As proposed by many, I made the overall CMDT Public
and the single record that comes with the package Protected
. This way my app is open for customer records but I own my records and keep them behind a packaged setup UI.
Furthermore, I chose Field Manageability settings for the fields to be SubscriberEditable.
Updating such a record from a packaged setup UI in the subscriber org, fails with:
Not allowed to install or modify metadata via Apex
I am strongly convinced it is not a permission issue. Although the Customize Application
and other permissions from the packaged Permset are lost mostly due to this Known Issue, the user still has a Full System Admin Profile which grants basically all rights.
I also make sure the DeveloperEditable
field isIgnored
during Metadata.Operations
:
public static void persist(MyCMDT__mdt record) {
String prefix = record.getSObjectType().getDescribe().getName().replace('__mdt', '');
Metadata.CustomMetadata cmd = new Metadata.CustomMetadata();
cmd.fullName = prefix + '.' + record.QualifiedApiName;
cmd.label = record.MasterLabel;
for (String fieldName : record.getPopulatedFieldsAsMap().keySet()) {
if (isIgnored(fieldName)) {
continue;
}
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = fieldName;
customField.value = record.get(fieldName);
cmd.values.add(customField);
}
Metadata.DeployContainer container = new Metadata.DeployContainer();
container.addMetadata(cmd);
Metadata.Operations.enqueueDeployment(container, new DeployCallback());
}