Mass/Bulk Insert Custom MetaData Records through CSV | Salesforce Developer Guide
Why do We need to Mass Insert Custom MetaData Records?
Basically in business, there are lots of perspectives when we need to use static values in more than one feature like while creating the records or using in the authorization. But why we need to insert in bulk comes when there is a business for a global organization or international levels like Global Hunger Index records, health issues like COVID, Malaria and many others. So this guide will be helpful in the above scenario.
How Can We Do This in Salesforce?
In Salesforce, we have a Custom Metadata feature that doesn’t have governor limits, because as we know Salesforce has a governor limit on performing the DML operation on the data that we’re storing into the custom object but on custom metadata, we’re not able to apply DML Operation directly on Custom MetaData and one more advantage for using custom metadata is that we can add records of custom metadata directly either in package or in the changeset.
So to perform the bulk operations on custom metadata using CSV files, we have four ways that we can make use according to our business perspective.
All the ways below use CSV (.comma separated file only, not UTF-8 with Comma-separated file).
Don't forget to check out: Back Up Metadata to Secure and Restore Your Customizations | Salesforce Guide
Way 1: Custom Metadata Loader
forcedotcom/CustomMetadataLoader: Tool to help users bulk create and update custom metadata records in salesforce.com from a CSV file. (github.com) - ( https://github.com/forcedotcom/CustomMetadataLoader )
This is a package.xml file that contains a Visualforce tab with apex controller classes. To deploy this in your org is a straightforward thing, you just need to click on deploy to Salesforce on the attached GitHub repository link and then authorize org either to sandbox org to production org.
Limitation of the above way was that API Name isn't correct issue will occur sometimes either it was correctly added into your CSV file and also the custom metadata field order will be changed.
Way 2: CSV to Custom MetaData Flow Screen Component
Flow Screen Component: Create/Update Custom Metadata type Records using CSV and Flow | forcePanda (wordpress.com) - ( https://forcepanda.wordpress.com/2021/02/23/flow-screen-component-create-custom-metadata-type-records-using-csv-and-flow/ )
This is an unmanaged package that contains a screen component (Lightning Aura Component) with apex controller class, but you just need to make a screen flow and add the FSC_CSVToCMDTRecords aura component in your screen flow.
Now you need to upload your CSV file into the Files object and copy the record id of the uploaded file record as well as also need to copy your Custom Metadata API Name.
After that, you need to paste both file recordId and Metadata API Name in the flow builder when you’re adding the FSC_CSVToCMDTRecords component on your screen flow.
The limitation of the above ways is that the Comma issue means if your CSV file contains the values with ,;!@$% then the comma values consider it as the next column value. So please consider this according to your business perspective.
Way 3: VS Code SFDX CLI Command
cmdt Commands | Salesforce CLI Command Reference | Salesforce Developers. - ( https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_cmdt.htm#cli_reference_force_cmdt_record_insert )
This is an officially documented process using SFDX CLI commands. You just need the VSCode with SFDX CLI installed in your system fine. Command to bulk insert record through CSV was
sfdx force:cmdt:record:insert -f path/to/my.csv -t My_CMDT_API_Name
Prepare your CSV File with the below considerations:-
We need to add a Name column to reference the DeveloperName identifier defaults to the column Name and is a required column as well as also it needs to be unique just like auto number field.
The limitation of the above way is that we are firstly inserting the records into our local repository or we can say we’re making a .xml file for every row that is in our CSV file. After that, we either need to deploy in our org one by one or we can deploy the whole metadata folder from VS Code and it takes too much time if the CSV file has more than 5K+ records.
Check out another amazing blog by Prafull Jain here: Docusign E-Signature in Salesforce | The Integration Guide
Way 4: Other approach was Apex Class
In this we can use below apex class with metadata bulk insertion method, but the limit is of 50 records at a time due to metadata enqueue LIMIT Exception and we also need to convert our CSV file to inline JSON first using https://csvjson.com/csv2json then replace \n with space & ' to \' and then we're ready to put that string into our apex class.
public class CustomMetaDataInsertion { public class WrapperCls { //For Test Result Custom MetaData public String Label; public String FIELD_NAME1; } public static void startInsertion(){ String jsonStr = '[{"Label":"TR10001","FIELD_NAME1":"2014-04-28"},{"Label":"TR10010","FIELD_NAME1":"2009-11-13"}]'; List<WrapperCls> mapRes=(List<WrapperCls>)JSON.deserialize(jsonStr,List<WrapperCls>.class); System.debug('Res '+mapRes.size()); for(WrapperCls rec:mapRes){ insertbulkMetadata(rec.Label,rec.FIELD_NAME1); } } public static void insertbulkMetadata(String labelVal,String fieldVal1){ try{ Metadata.DeployContainer mdContainer = new Metadata.DeployContainer(); //First Record Metadata.CustomMetadata firstMetadataRec = new Metadata.CustomMetadata(); firstMetadataRec.fullName = 'METADATA_API_NAME__c.'+String.escapeSingleQuotes(labelVal); firstMetadataRec.label = String.escapeSingleQuotes(labelVal); //adding values to fields Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue(); customField1.field = 'FIELDNAME1'; customField1.value = String.escapeSingleQuotes(fieldVal1); firstMetadataRec.values.add(field1); //Add more fields as you want mdContainer.addMetadata(firstMetadataRec); //adding record container that will be used to deploy the records in custom metadata. 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('Error while creating modifying custom metadata. '+ex.getCause()+' '+ex.getMessage()); } } }
In CSV we need to add one more column as Label for DeveloperName reference & that needs to be unique.
Responses