Tuesday, December 26, 2023

Export all Custom Metadata Records in CSV file using Apex

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.

Open developer console and open execute anonymous window. Execute below script:

SK_CustomMetadataUtility.exportAllCMRecordsInCSV();


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!!