In this blog, we will learn about Custom Metadata Types in Salesforce. Custom metadata in Salesforce refers to customizable, deployable, and packageable application metadata. Custom metadata types are objects used to define the structure of application metadata in Salesforce. The fields and values within custom metadata types consist solely of metadata, not data. The records of custom metadata types are also considered metadata. One advantage of using metadata is the ability to import it into Salesforce and modify it as needed.
After creating a custom metadata type, developers can build reusable functionality based on the metadata of that type. This allows for dynamic behavior in the application based on the configuration of custom metadata. For Ex. : We can use any custom metadata in apex, validation rules, flows etc
Custom metadata types in Salesforce are exposed in the application cache, allowing for efficient access without repeated queries to the database. This makes custom metadata readily available for various purposes such as formula fields, validation rules, flows, Apex, and SOAP/REST API.
Benefits of using Custom Metadata :
- First, it is customizable, allowing developers to tailor it to specific requirements.
- Second, it is deployable, meaning it can be easily moved between different environments or orgs.
- Third, it is packageable, allowing it to be included in packages and distributed to other Salesforce orgs.
- Fourth, custom metadata is upgradeable, enabling developers to make changes to the metadata structure while preserving the existing records.
Now let’s get our hands dirty with real time examples :
- How to Create Custom Metadata Types :
- Search “Metadata” in Quick Find ➤ Click on “Custom Metadata Types” ➤ Create New(just the way we create custom objects)
Create new fields as per the business need 🡻
Create new records for your business – just like we create custom object record 🡻
We have already created 1 sample record – you can create other records as well by clicking on “New” button based on your requirement 🡻
- How to use this custom metadata in real-time?
- Use Custom Metadata in Apex :
- 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.
- Normal SOQL – It works the same as it works in the case of custom/standard object records, it counts in governor limits as well
- Use Custom Metadata in Apex :
// Code for getAll() functionality
List<HTTP_Callout_Configuration__mdt> mcs = HTTP_Callout_Configuration__mdt.getAll().values();
system.debug('mcs--->> ' +mcs);
// Code for getInstance() functionlity
// It will only return particular record
// Using getInstance() with Developer Name
HTTP_Callout_Configuration__mdt mc = HTTP_Callout_Configuration__mdt.getInstance('Oracle_Callout');
system.debug('mc ==>> ' + mc);
// Using getInstance() with RecordId
HTTP_Callout_Configuration__mdt mcId = HTTP_Callout_Configuration__mdt.getInstance('m022x000000cXLM');
system.debug('mcId ==>> ' + mcId);
Use Custom Metadata in Flow :
Use Custom Metadata in Validation Rules/Formula Fields :
Considerations of Using Custom Metadata :
- If the text value that is being retrieved is more than 255 characters then we have to retrieve it with the help of SOQL – getAll() & getInstance() method will not work there
- You can create up to 200 custom metadata types per Salesforce org.
- You can create up to 100 custom fields for each custom metadata type object.
What if you want to insert/update in bulk?
- Well, bulk insertion/updation cannot happen via normal tools like data loader or workbench – so there is a special tool built for it.
- Just download the tool from this Github Link and follow the steps given and use it in your org/Sandbox
Additional Info :
- Here is a link of Trailhead for getting more info and badges
- Official Documentation