5

I am trying to play with the new Summer'17 feature of Deploying and Retrieving Metadata through Apex. I was trying to use the method Metadata.Operations.enqueueDeployment to update existing custom metadata values in the org. Here is how my Custom Metadata looks like -enter image description here

These are the values present in the Custom Metadata -enter image description here

Below is the code snippet, where I am trying to update the value (Enabled checkbox to false) for Process Builder Preference.

Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
customMetadata.fullName = 'Admin_Preference__mdt.Process_Builder_Preference';

Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = 'Enabled__c';
customField.value = Boolean.valueOf('false');
customMetadata.values.add(customField);

Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(customMetadata);

MyDeployCallback callback = new MyDeployCallback();

// Enqueue custom metadata deployment
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback); 

Here is the MyDeployCallback class -

public class MyDeployCallback implements Metadata.DeployCallback {
    public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
            System.Debug('--++-- Deployment is successful');
        } else {
           System.Debug('--++-- Deployment failed with status: ' + result.status + ' with error message: ' + result.errorMessage);
        }
    }
}

From log what I am getting is: -

11:43:17.8 (9856354)|USER_DEBUG|[7]|DEBUG|--++-- Deployment failed with status: Failed with error message: null

Any idea what wrong is going on here?

8

There can be several reason of this deployment error. Possible reason is mandatory field is missed or the referenced metadata name is incorrect. Below is the working code to create and update Custom Metadata via Metadata namespace.

Callback class:-

public class CustomMetadataCallback implements Metadata.DeployCallback {
    public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
            System.debug(' ===>'+ result);
        } else {
            // Deployment was not successful
            System.debug(' ===> fail '+ result);
        }
    }
}

Metadata Creation Script:-

    // Set up custom metadata to be created in the subscriber org.
    Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
    customMetadata.fullName = 'Response_Type.NotFound_Code';
    customMetadata.label = 'NotFound_Code';

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'Status_Code__c';
    customField.value = '404';

    customMetadata.values.add(customField);

    Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
    mdContainer.addMetadata(customMetadata);

    // Setup deploy callback, MyDeployCallback implements
    // the Metadata.DeployCallback interface (code for
    // this class not shown in this example)
    CustomMetadataCallback callback = new CustomMetadataCallback();

    // Enqueue custom metadata deployment
    Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);

Detail:-
1. Metdata Name is Response_Type__c it has a field called Status_Code__c
2. Above we are trying to add new Record to response named "Not Found".

Things missed in question's code:-
1. Metadata name does not require __mdt.
2. MasterLabel field is not mentioned.

  • 1
    Awesome Ashwani. It is working now. The problem is with label field missing. fullName can have _mdt and it is working fine. – Sudipta Deb Apr 28 '17 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.