Custom Metadata Types in Salesforce

Custom Metadata Types

In this blog, we will be discussing everything about custom metadata types and how we can leverage them to build dynamic applications on the Salesforce Platform. 

Let’s get started with the basics first! If you’re familiar with the Salesforce Platform, you know that Data in Salesforce is stored in Objects. An Object in Salesforce is nothing but a Database table which has columns and rows. Columns are fields and rows are the records. 

What is Custom Metadata Types?

The term “Metadata” is nothing but data about data. In simple words, when you are creating a record within Salesforce, you are creating data as well as metadata. The data which you are trying to enter, is captured or stored in the fields which are nothing but columns of your Object. 

The fields in your objects are metadata and the data within those fields(or metadata) are data. That’s the reason why you would often hear metadata to be defined as data about data. I hope I was able to make that simple for you! 

Custom Metadata Types in Salesforce are similar to custom objects. It has a suffix of “__mdt” instead of “__c” in the API namespace. Let’s say if you create a Custom Metadata type with name “Apex Hours”, the API name of the metadata type would appear as – “Apex_Hours__mdt”.

It allow you to build dynamic applications using Apex on the Salesforce Platform and also enables you to have more control over the execution of your code. 

Custom Metadata vs Custom Objects

You might be already asking yourself this question in your mind – “We have Custom Objects already, so what is the need to use this?” Let’s get straight to it. 

  1. Custom Metadata Types stores the records in a memory cache which allows faster retrieval of data when you execute a query. You can observe a huge runTime difference, when you query/retrieve metadata records vs custom object records.
  1. To keep your Salesforce Org performant you can consider using it. Additionally, this provides you the flexibility to have more control over logic based execution in Apex. 
  2. This doesn’t count against Governor limits. 
  1. It are deployable from one environment(Org) to another environment. You can package your metadata along with data or deploy it easily from one Org to another. 
  1. ISVs rely heavily on using it while developing robust and scalable AppExchange applications. 
  1. Custom objects on the other hand are not mem-cache based. Execution of query takes longer if you have huge data in your objects and if the Query is non-selective. Querying against records from Objects are counted into SOQL governor limits per transaction. 
  1. Protected Custom Metadata Types can be created to store sensitive information like Client Secret, Client ID, Tokens and many more. (protected_x property that describes whether the custom metadata record is a protected component.)

Keeping the above factors in mind, you can consider developing your application with Custom Metadata Types. 

How to use custom metadata in Apex

Custom metadata types methods are instance type methods and are called by and operate on a specific instance of a custom metadata type.

The following methods are available in Apex to fetch it:

  1. getAll() – Returns a map containing custom metadata records for the specific custom metadata type. The map keys are the record DeveloperNames and the map values are the record sObjects.
  1. getInstance(recordId) – Returns a single custom metadata type record sObject for a specified record ID.
  1. getInstance(developerName) – Returns a single custom metadata type record sObject for a specified developerName field of the custom metadata type object.

Let’s go ahead and create a Custom Metadata Type in our org and reference the same in Apex. 

Create Custom Metadata Types

Login to your Org, go to setup → In the quick find search for “Custom Metadata Types”. 

Click on “New Custom Metadata Type”. 

Create Custom Metadata Types
Create Custom Metadata Types

Fill in the details – label name, description etc and then click on “Save”. Congrats, you have just created your first Custom Metadata Type record

Now let’s go ahead and create a record for your new custom metadata type. Click on the “Manage” button to create records for your metadata type.

So, we have successfully created a record as you see above. Now let’s go ahead and try to retrieve the record values Apex. 

We leverage the getAll() method in order to retrieve the record values. 

List<Apex_Hours__mdt> mcs = Apex_Hours__mdt.getAll().values();

If you want to fetch a specific record, you can use the getInstance() method. The method accepts recordIDs as a parameter. 

Apex_Hours__mdt mc = Apex_Hours__mdt.getInstance('Insert recordID here);

Go ahead and create a few records and play around in your Developer Org. 🙂 

Let’s also have a look at what are the differences between a Custom Metadata Type and a Custom Setting. 

Differences between Custom Metadata Types and Custom Settings

Custom Settings are also similar to custom objects. You can either create a List based or hierarchy based custom setting. Custom metadata does not support Hierarchy type of data based on user profile or a specific user.

  1. Custom settings don’t use the application cache and are not efficient when compared with custom metadata types. 
  1. Custom settings cannot be deployed using packages or Metadata API/Change Sets. Whereas custom metadata types can be easily packaged and deployed. Metadata types can also be deployed via Change Sets/Metadata API. 
  1. Querying custom setting records count against the SOQL governor limit of the platform and can have performance impacts if the data is too huge. 
  1. Custom settings have to be created manually as they are not deployable. 
  1. Custom settings do not support relationship fields. You can create lookups between Custom Metadata objects.
  1. Custom setting data is not visible in test classes whereas custom metadata types are visible in test class without the “SeeAllData” annotation.

Custom Metadata Types in Salesforce Video


I hope you have learnt a little something here in this blog post, as we discussed custom metadata types and also the difference between custom metadata types and custom settings. More amazing content to follow, keep an eye on this space!! 🙂

Abhirup Mukherjee

Abhirup Mukherjee

Abhirup work as a Senior Technical Engineer at Salesforce. (Ex - Salesforce Lightning Champion)

Share this article

Leave a reply

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,541 other subscribers

Our Supporter


Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!