1

I am unable to mark custom metadata type's field of type checkbox to true from apex code.

public class PRTestCtrl{

@AuraEnabled
public static void saveCMD()
{
    // Set up custom metadata to be created in the subscriber org.
    Metadata.CustomMetadata cmd =  new Metadata.CustomMetadata();
    cmd.fullName = 'Audit_Object_Setting.md212e';
    cmd.label = 'md2label12e';

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'Enable_Audit_History__c';
    customField.value = true; //have tried 'true'
    Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue();
    customField1.field = 'Object_Name__c';
    customField1.value = 'Location__c';
    cmd.values.add(customField1);

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

    CustomMetadataCallback callback = new CustomMetadataCallback();

    Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
}

   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);
            }
        }
    }

}

When this custom metadata type record is saved, the value for 'Enable_Audit_History__c' is set to false on the UI and in the result of SOQL.

5
  • You have to also call the deploy .Can you paste the full code Mar 19, 2018 at 12:14
  • 1
    Yes you need to actually deploy it. But also your fullName format is wrong. To upsert a Custom Metadata records of type Audit_Object_Setting__mdt with DeveloperName = auditobj, you should set fullName = Audit_Object_Setting.auditobj.
    – Charles T
    Mar 19, 2018 at 12:17
  • worth considering the Andrew Fawcett library to make this easier and loggable
    – cropredy
    Mar 19, 2018 at 18:06
  • I added the method with the deploy instruction.
    – SF Learner
    Mar 19, 2018 at 22:55
  • I am still at where I was before with this issue. Can you confirm this is not a bug ?
    – SF Learner
    Mar 21, 2018 at 3:32

1 Answer 1

1
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'Enable_Audit_History__c';
    customField.value = true; //have tried 'true'
    Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue();
    customField1.field = 'Object_Name__c';
    customField1.value = 'Location__c';
    cmd.values.add(customField1);

customField is never added to the deployment container. You add cmd.values.add(customField1); but not the first field

1
  • cannot believe I missed that, silly me :(
    – SF Learner
    Mar 21, 2018 at 22:06

You must log in to answer this question.

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