0

I have a controller that updates/creates metadata type records. It works fine for the creation of new records, but when I try to update existing records, I get this error

duplicate value found: ObjectAPIName__c duplicates value on TestDeploy2

and when I remove this field from my code, I get this error

'Required fields are missing: [ObjectAPIName__c]'

Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = 'ForgotCustomerStrategySettings.' + fullName;
customMetadata.label = fullName;
    
Metadata.CustomMetadataValue customField2 = new Metadata.CustomMetadataValue();
customField2.field = 'ObjectAPIName__c';
customField2.value = ObjectAPIName;

customMetadata.values.add(customField2);
    
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();

mdContainer.addMetadata(customMetadata);
    
PostInstallCallback callback = new PostInstallCallback();  
           
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);

1 Answer 1

1

We have installer code that does this sort of thing. The only difference I can see between what we do and what you have is that in order to update a custom metadata record we first retrieve it using (adjusted to use your full name prefixing):

Metadata.Operations.retrieve(Metadata.MetadataType.CustomMetadata, new String[] { 'ForgotCustomerStrategySettings.' + fullName });

If the returned list doesn't contain the Metadata.CustomMetadata instance with the required full name you need to create it, otherwise you should simply update that instance before enqueuing a deployment to update it.

You must log in to answer this question.

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