Custom Metadata

What is Custom Metadata :
Custom metadata is very similar to list type custom settings with some differences.

  • First being that custom metadata records can be deployed to production, unlike the custom setting records which need to be created again in production.
  • We can create validation rules on custom metadata unlike the list custom settings.

Creating a Custom Metadata
We need to go to setup and search for custom metadata.

Then, open the page and click on the ‘New’ button which takes us to the Custom Metadata creation screen.

meta-data-type

Once we have created this custom metadata, we will see a button called ‘Manage My ’ which will allow us to create records for this custom metadata :

While creating the records for a custom metadata, we can see that it has two required fields:

  1. Label
  2. Name

Both of these can be used to fetch the values of this custom metadata in code. The Name is a unique field, while the Label is not.

And just like the custom settings, we can add custom fields on a custom metadata too.
Accessing Custom Metadata in Code
The custom metadata is appended with ‘__mdt’ while accessing it on code. In order to fetch it in code, we need to do a SOQL on the custom metadata object.

For example :

list listofCMDT = [SELECT MasterLabel,DeveloperName FROM My_First_Metadata__mdt];

Here, the MasterLabel is the api name of the Label field and the DeveloperName is the api name for the Name field.

Differences between Custom Setting and Custom Metadata:

Custom Settings Custom Metadata
1.Custom settings can be created via apex, just like sobject records. 1.Custom metadata cannot be created via apex.
2.Custom setting records cannot be deployed to production directly, we need to create the same records in production as well. 2.Custom Metadata records can be directly deployed to production.
3.Custom Settings have two types List and Hierarchy. 3.Custom Metadata has only one type.
4.Custom Settings can be accessed in code without needing to do SOQL by using the getAll() method. 4.In order to access Custom Metadata in apex, we need to do an SOQL query.
5.Custom Settings do not support validation rules. 5.Custom Metadata supports validation rules.
Subscribe Now