+ Start a Discussion
JonathanFox UKJonathanFox UK 

Create Custom Metadata - Apex Test Class

Hi all,

I use the Metadata API to update a metadata record similar to the example in this blog -> https://www.sfdcpanther.com/create-update-custom-metadata-using-apex/

However, to test one of my apex methods, I pass in some new details such as preferences for the end-user to change via a LWC rather than going into set up > Custom Metadata.

This works fine however to write a test method for this method, I am unsure how I can get code coverage as we are unable to deploy metadata in a test method.

My method of which I wish to get coverage is below:
    public static void updateConfig(String companyName, String publicKey, Boolean initialSearchPref, Boolean resubmitSearchPref, String CRA1, String CRA2){
        List<String> metaDataFullNameList = ConfigController.getMetaDataFullNameList();

        Metadata.CustomMetadata customMetadataRecord = new Metadata.CustomMetadata();
        customMetadataRecord.fullName = metaDataFullNameList[0];
        System.debug('customMetadataRecord.fullName > ' + customMetadataRecord.fullName);

        if(publicKey != null){
            Metadata.CustomMetadataValue updateMetadataField1 = new Metadata.CustomMetadataValue();
            updateMetadataField1.field = 'Public_Key__c';
            updateMetadataField1.value = publicKey;
        Metadata.CustomMetadataValue updateMetadataField2 = new Metadata.CustomMetadataValue();
        updateMetadataField2.field = 'Automatically_Submit_Search__c';
        updateMetadataField2.value = initialSearchPref;

        Metadata.CustomMetadataValue updateMetadataField3 = new Metadata.CustomMetadataValue();
        updateMetadataField3.field = 'Automatically_Resend_Referred_Searches__c';
        updateMetadataField3.value = resubmitSearchPref;

        if(CRA1 != null){
            Metadata.CustomMetadataValue updateMetadataField4 = new Metadata.CustomMetadataValue();
            updateMetadataField4.field = 'CRA_1__c';
            updateMetadataField4.value = CRA1;

        if(CRA2 != null){
            Metadata.CustomMetadataValue updateMetadataField5 = new Metadata.CustomMetadataValue();
            updateMetadataField5.field = 'CRA_2__c';
            updateMetadataField5.value = CRA2;
        if(companyName != null){
            Metadata.CustomMetadataValue updateMetadataField6 = new Metadata.CustomMetadataValue();
            updateMetadataField6.field = 'Company_Name__c';
            updateMetadataField6.value = companyName;

        customMetadataRecord.label = 'Live';
        System.debug('customMetadataRecord > ' + customMetadataRecord);

        Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
        System.debug('mdContainer > ' + mdContainer);

       ConfigMetaDataCallback callback = new ConfigMetaDataCallback();
        // Enqueue custom metadata deployment
        Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
        System.debug('jobId > ' + jobId);

AnudeepAnudeep (Salesforce Developers) 
As per my understanding, you should essentially test a Metadata Service class. I recommend looking at the example posted here
Mosh HamedaniMosh Hamedani
According to my arrangement, you ought to basically test a Metadata Service class. I suggest taking a gander at the model posted 
Best Regards Image Restoration Services