I am working on a feature that deploys custom metadata record. I am using a scratch org with a linked namespace.

This is a my method which initiates deployment:

public static String deployDemo()
    Metadata.CustomMetadata customMetadataRecord;
    List<String> componentNameList = new List<String>{'mynamespace__ObjectSetting__mdt.mynamespace__Config'};
    List<Metadata.Metadata> components = Metadata.Operations.retrieve(Metadata.MetadataType.CustomMetadata, componentNameList);
    customMetadataRecord = (Metadata.CustomMetadata)components.get(0);

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'mynamespace__Objects__c';
    customField.value = 'dummy text';

    Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
    ID jobID = Metadata.Operations.enqueueDeployment(mdContainer, new MetadataDeployCallback());

    return String.valueOf(jobID);

And here the implementation of Metadata.DeployCallback:

public with sharing class MetadataDeployCallback implements Metadata.DeployCallback
    public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context)
        System.debug('MetadataDeployCallback with DeployResult ' + JSON.serialize(result));
        System.debug('MetadataDeployCallback with DeployCallbackContext ' + JSON.serialize(context));

        MetadataDeployComplete__e mdEvent = new MetadataDeployComplete__e();
        mdEvent.Success__c = result.success;
        mdEvent.JobID__c = result.id;
        mdEvent.Status__c = String.valueOf(result.status);

        if(result.status != Metadata.DeployStatus.Succeeded)
            // Verbose error handling is skipped for brevity
            mdEvent.ErrorMessage__c = 'Error';

        Database.SaveResult eventResult = EventBus.publish(mdEvent);
        if( !eventResult.success)
            system.debug('Failed to publish event');


Every time I call the deployDemo() method I get a valid jobID value. However, the handleResult from the MetadataDeployCallback class is invoked only when deployment is Successful. If a deployment has been completed with Fatal Error, handleResult is never executed (this is my observation based on absence of debug logs ). What is more, this issue occurs randomly, sometimes deployment is successful, sometimes it ends up with fatal error.

enter image description here

enter image description here


Why is Metadata.DeployCallback not called when deployment status is Fatal Error? And why does deployments fail randomly? If there was an error in the code, I would expect it to fail all the time.


Cannot update packaged custom metadata records from packaged Apex

Edit Custom Metadata Records from a Managed Package

Cannot update Custom Metadata records from Subscriber org in managed app

  • Have you opened Salesforce support case?
    – Patlatus
    Commented May 10, 2019 at 12:20
  • No, because I don't have a Salesforce org, unfortunately. I use free developer orgs, which are not covered by support.
    – Eduard
    Commented May 10, 2019 at 13:27
  • @Patlatus, do you know, by chance, how can I reach support in my case? Is it even possible?
    – Eduard
    Commented May 11, 2019 at 7:52
  • They would refuse the ticket if you don't open it from client account with premier support
    – Patlatus
    Commented May 13, 2019 at 7:40
  • Yes, that's why I cannot reach out to support.
    – Eduard
    Commented May 14, 2019 at 6:55


You must log in to answer this question.

Browse other questions tagged .