Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Insert and Update multiple custom metadata records through Salesforce Apex.

    Dhirendra Feb 11, 2020
    38,080  Views

    Hi Folks, as we all know Salesforce has limitations while performing any kind of DML Operation on custom metadata (.mdt) objects records. Even though there is a limitation we have a way to create and update custom metadata records using Apex.

    dont miss out iconDon’t forget to check out: Salesforce Apex Trigger – Child to Parent Trigger using Map

    Let us begin with one custom Metadata Object sample for which we will be creating and updating records using apex. Below is the image for one such custom metadata.

    metadata object sample

    As shown in the image we have our sample custom metadata object with three Custom Fields now we will be creating multiple records for custom metadata by providing values for the label, along with the three custom fields on the object using Apex. You can use the below-provided Apex Class to perform the insertion and update of custom metadata records.

    /**
     *@Desc: A class that can be used to insert and update multiple custom metadata records in a single transaction. 
     **/
    public class BulkUploadMetadata {    
        /**
         *@desc: A method to insert multiple records for custom metadata object in a single go. 
    	 **/
        public void insertbulkMetadata(){
            try{
                Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
                String nameSpacePrefix =''; // if the metadata belongs to any package than include the namespace.                
                //First Record 
                Metadata.CustomMetadata firstMetadataRec =  new Metadata.CustomMetadata();
                firstMetadataRec.fullName = nameSpacePrefix + 'Bulk_Upload__mdt.FirstRec';
                firstMetadataRec.label = 'FirstRec';           
                //adding values to fields
                Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue();
                customField1.field = 'RecName__c';
                customField1.value = 'FirstRecordName';
                firstMetadataRec.values.add(customField1);            
                Metadata.CustomMetadataValue customField2 = new Metadata.CustomMetadataValue();
                customField2.field = 'Is_Displayed__c';
                customField2.value = True;
                firstMetadataRec.values.add(customField2);                       
                Metadata.CustomMetadataValue customField3 = new Metadata.CustomMetadataValue();
                customField3.field = 'NameType__c';
                customField3.value = 'First';
                firstMetadataRec.values.add(customField3);                        
                mdContainer.addMetadata(firstMetadataRec);  //adding record container that will be used to deploy the records in custom metadata.                        
                //Second Record 
                Metadata.CustomMetadata secondMetadataRec =  new Metadata.CustomMetadata();
                secondMetadataRec.fullName = nameSpacePrefix + 'Bulk_Upload__mdt.SecondRec';
                secondMetadataRec.label = 'SecondRec';           
                //adding values to fields
                Metadata.CustomMetadataValue secondCustomField = new Metadata.CustomMetadataValue();
                secondCustomField.field = 'Is_Displayed__c';
                secondCustomField.value = True;
                secondMetadataRec.values.add(secondCustomField);            
                Metadata.CustomMetadataValue secondCustomField2 = new Metadata.CustomMetadataValue();
                secondCustomField2.field = 'NameType__c';
                secondCustomField2.value = 'Second';
                secondMetadataRec.values.add(secondCustomField2);           
                Metadata.CustomMetadataValue secondCustomField3 = new Metadata.CustomMetadataValue();
                secondCustomField3.field = 'RecName__c';
                secondCustomField3.value = 'Second RecordName';
                secondMetadataRec.values.add(secondCustomField3);               
                mdContainer.addMetadata(secondMetadataRec);            
                system.debug('mdContainer**'+mdContainer);            
                // Enqueue custom metadata deployment
                // jobId is the deployment ID
                Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);
                system.debug('jobId***'+jobId);
            }
            catch(Exception ex){             
                //System.assert(false,ex.getMessage()); 
                system.debug('Error while creating modifying custom metadata.');
            }
        }
        /**
         *@desc: A method to bulk update the custom metadata records . 
         **/
        public void updateBulkMetadata(){
            try{
                Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
                for(Bulk_Upload__mdt objMetadata :[SELECT Id, DeveloperName,
                                               		  MasterLabel, Label, RecName__c,
                                               		  NameType__c, Is_Displayed__c
                                               FROM Bulk_Upload__mdt]){
                    Metadata.CustomMetadata metadataRec =  new Metadata.CustomMetadata();
                    metadataRec.fullName = 'Bulk_Upload__mdt.'+objMetadata.DeveloperName;
                    metadataRec.label = objMetadata.MasterLabel;
                    Metadata.CustomMetadataValue customFieldtoUpdate = new Metadata.CustomMetadataValue();
                    customFieldtoUpdate.field = 'Is_Displayed__c';
                    customFieldtoUpdate.value = false;
                    metadataRec.values.add(customFieldtoUpdate);
                    mdContainer.addMetadata(metadataRec);
                }
                system.debug('mdContainer**'+mdContainer);            
                // Enqueue custom metadata deployment
                // jobId is the deployment ID
                Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);
                system.debug('jobId***'+jobId);                                   
            }catch(exception ex){
                system.debug('exception '+ex.getMessage());                                 
            }       
        }    
    }

    a. Custom Metadata Record Creation:

    In order to create new records we need to execute the insertBulkMetadata() method using the anonymous debug block from developer console application as shown in the below image:

    Insert Bulk Metadata

    Once the above-highlighted code snippet is executed the two new records in the custom metadata object will be created as shown in the below image.

     custom metadata setup

    b. Record Update:

    We can also update the same inserted records using updateBulkMetadata() from the above-provided Apex Class. In case of an update, we will uncheck the “is Displayed” field from the records. In order to perform updating of records, we need to execute the following code snippet in the apex debug anonymous block.

    Metadata Updation using Apex

    once the above-highlighted code snippet is executed all the existing records in the custom metadata object will be updated. The “Is Displayed” field value changes from true to false as shown in the below image.

    Custom Meta Data Types Setup

    Categories: Salesforce Apex
    Tagged: Custom Fields, DML Operation, Metadata, records, Salesforce Apex Class, Salesforce Developer Console, Salesforce Objects, Salesforce Org

    Get listed your company

    Have an innovative Salesforce solution that delivers faster, smarter results?
    Join the Marketplace

    [adinserter block=”16″]

    best salesforce consultants
    best salesforce consultants
    best salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    salesforce consultants
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.