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.
- 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.
- 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.
- This doesn’t count against Governor limits.
- 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.
- ISVs rely heavily on using it while developing robust and scalable AppExchange applications.
- 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.
- 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:
- 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.
- getInstance(recordId) – Returns a single custom metadata type record sObject for a specified record ID.
- 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”.
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(); system.debug('mcs---'+mcs);
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.
- Custom settings don’t use the application cache and are not efficient when compared with custom metadata types.
- 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.
- Querying custom setting records count against the SOQL governor limit of the platform and can have performance impacts if the data is too huge.
- Custom settings have to be created manually as they are not deployable.
- Custom settings do not support relationship fields. You can create lookups between Custom Metadata objects.
- 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!! 🙂