• Adam Lederman

    First off… Thank you for all you do! Your posts are by far my go to resource when I get stumped by a Salesforce gotcha…

    I needed to modify this to create a bulk deployment, instead of multiple deployments as my app was to help mass update metadata values. The method to add for Update is:

    public static void bulkifyMetadataDeployment(List nam, Map<String,Map> mastMap ){
    Metadata.DeployContainer mdContainerBulk = new Metadata.DeployContainer();

    For(String s : nam){
    String hte = String.valueOf(s);
    map metadataFieldValueMap = mastMap.get(hte);
    String metdataName = s.substringBefore(',');
    String remString = s.substringAfter(',');
    String recordDevName = remString.substringBefore(',');
    String label = s.substringAfterLast(',');

    Metadata.CustomMetadata cMetadata = new Metadata.CustomMetadata();
    cMetadata.fullName = metdataName + '.' + recordDevName;
    cMetadata.label = label;

    for(String key : metadataFieldValueMap.keySet()){
    Metadata.CustomMetadataValue cMetadataValue = new Metadata.CustomMetadataValue();
    cMetadataValue.Field = key;
    cMetadataValue.Value = metadataFieldValueMap.get(key);
    cMetadata.values.add(cMetadataValue);
    }
    mdContainerBulk.addMetadata(cMetadata);
    }
    CustomMetadataUtils callback = new CustomMetadataUtils();
    Id jobId = Metadata.Operations.enqueueDeployment(mdContainerBulk, callback);
    }

    you need to modify the values you pass in as well..

    you need add the following to your method which will call the utility:

    String dlmiter = ',';
    Map<String,Map> mastMap = new Map<String,Map>();
    List knam = new List();
    map bne = new map();


    /*
    This is to bulk the deployment process so you will loop through a list of the MDT you want to update
    */

    For(CustomMetadataObject__mdt cmd : LIST_of_MDT){

    String metadataNam = 'CustomMetadataObject__mdt';
    String recordDevNam = cmd.DeveloperName;
    String label = cmd.MasterLabel;

    //create the String that will be the Key in the map<String,Map>

    String threeVarIn = metadataNam + dlmiter + recordDevNam + dlmiter + label;
    /*
    *add the string to the list 'knam' - you will loop through this list in the utility to build the individual *metadata records.
    */
    knam.add(threeVarin)

    //Remove the Call to the Utility which is the third line in the example provided

    /* EXAMPLE Update Before Bulkify
    * Map metadataFieldValueMap = new Map();
    * metadataFieldValueMap.put('TaxPercent__c', 15);
    * CustomMetadataUtils.updateCustomMetadata('SalesTaxSetting__mdt','Value_Added_Tax', 'Value Added Tax',metadataFieldValueMap);
    */

    Map metadataFieldValueMap = new Map();
    metadataFieldValueMap.put('MDT_Field__c', valuetoset);

    //Replace the Call on the third line and put the Key String and the Map into your Master Map

    mastMap.put( threeVarIn , metadataFieldValueMap );

    //close your for loop
    }
    //call the bulk method from the utility class

    CustomMetadataUtils.bulkifyMetadataDeployment(knam,mastMap);

    }

    Hope this helps to enhance the awesome utility that Biswajeet has provided!

    ANOTHER NOTE: If your MDT field is a Boolean field you will need to add two methods that replace the Map with Map in the utility.

  • Bhisam Mangla

    How we can get the Id of created/updated metadata record in apex.