Using apex, we can export all custom metadata records in CSV files. This is sometime needed if we want to review all custom metadata records after sandbox refresh or to check the dependency of any field or attribute in all custom metadata records.
Below is apex class which can be used for this and once you execute its method from developer console, you will receive email with csv that contain all custom metadata records.
public class SK_CustomMetadataUtility { | |
public static void exportAllCMRecordsInCSV(){ | |
Map<string,List<string>> objToFieldsMap = new Map<string,List<string>>(); | |
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); | |
for(String ss1: schemaMap.keyset()){ | |
Schema.SObjectType objToken=schemaMap.get(ss1); | |
//find details about sobject | |
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe(); | |
string objAPIName= string.valueof(objDescribe.getName()); | |
//filter object for which APi name ends with _mdt | |
//filter manage package objects as they contain __ twice in their API name | |
if(objAPIName.endsWith('__mdt') && objAPIName.countMatches('__')==1){ | |
//system.debug('***custom metadata name:'+objAPIName); | |
objToFieldsMap.put(objAPIName,new List<string>()); | |
//finding all fields of sobjects | |
Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); | |
objToFieldsMap.get(objAPIName).add('DeveloperName'); | |
objToFieldsMap.get(objAPIName).add('MasterLabel'); | |
for(String ss:Fieldmap.keyset()){ | |
Schema.DescribeFieldResult fd=fieldMap.get(ss).getDescribe(); | |
string fieldAPIName=string.valueof(fd.getName()); | |
//check if field is custom or not | |
if(fd.isCustom()){ | |
objToFieldsMap.get(objAPIName).add(fieldAPIName); | |
} | |
} | |
} | |
} | |
system.debug('**objToFieldsMap size:'+objToFieldsMap.size()); | |
system.debug('**objToFieldsMap:'+objToFieldsMap); | |
integer count =0; | |
string csvString = 'Name,DeveloperName,MasterLabel,Fields\n'; | |
for(string objAPIName: objToFieldsMap.keyset()){ | |
if(count<100){ //here we are restricting script to export only 100 custom metadata records | |
Map<string,List<string>> developerNameWithFieldValuesMap= new Map<string,List<string>>(); | |
String qryString = 'SELECT '+ string.join(objToFieldsMap.get(objAPIName),',') +' From '+objAPIName; | |
system.debug('**qryString-'+objAPIName+'-:'+qryString); | |
for(sobject sb: Database.query(qryString)){ | |
string developerName= string.valueof(sb.get('developerName')); | |
developerNameWithFieldValuesMap.put(developerName,new List<string>()); | |
for(string field: objToFieldsMap.get(objAPIName)){ | |
if(sb.get(field)!=null && field!='developerName' && field!='MasterLabel'){ | |
//Fields coulm will contains Field API and its corresponding value | |
//here &SK& is delimiter to differentiate the API name with value | |
developerNameWithFieldValuesMap.get(developerName).add(field+'&SK&'+sb.get(field)); | |
} | |
} | |
csvString = csvString + objAPIName+','+string.valueof(sb.get('DeveloperName')).escapeCsv()+','+ string.valueof(sb.get('MasterLabel')).escapeCSV()+','+ string.join(developerNameWithFieldValuesMap.get(developerName),',').escapecsv()+'\n'; | |
} | |
count= +1; | |
} | |
} | |
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment(); | |
blob csvBlob = Blob.valueOf(csvString); | |
string csvname= 'Custom metadata records.csv'; | |
csvAttc.setFileName(csvname); | |
csvAttc.setBody(csvBlob); | |
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage(); | |
String[] toAddresses = new list<string> {UserInfo.getUserEmail()}; | |
String subject = 'Custom metadata records -'+system.now(); | |
email.setSubject(subject); | |
email.setToAddresses( toAddresses ); | |
email.setPlainTextBody(subject); | |
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc}); | |
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); | |
} | |
} |
Open developer console and open execute anonymous window. Execute below script:
Below is snapshot of csv file:
Note:
- This script will specify the custom metadata field api name and its value seperated by "&SK&" delimiter.
- All fields and its values are seperated by comma.
- You can modify this method to fetch all custom metadata related to installed packages by specifying a parameter for namespace.
Hope this will help!!