Custom Metadata Types are a very powerful capability in the Salesforce platform, but many people don’t know what it is, or how to use it. I wanted to give some background and examples to help admins understand how it could be used in their orgs.
What are Custom Metadata Types?
Custom Metadata Types are similar to Custom Settings in Salesforce, except that they are deployable and packagable. The architecture consists of the metadata type, and custom records defined by the type. This means they are similar to a simple custom object. You create custom fields to match the needs of your business case. Then individual records can represent the necessary data.
Master Data Management – You can use Custom Metadata Types to store master data records to be used throughout an organization. Some common use cases I have seen are storing org-wide VAT or tax calculations, and partner tier thresholds. In the case of tax calculations, relationships between country and state are created to store individual tax rates. Apex code or formula fields can access the metadata records to perform the calculations.
Business Processes – Validation or routing of records is handled by specific business logic, and needs to be controlled org-wide.
Protected Information – Store data like API keys in a protected package.
Relationship Mapping – Control relationships like assigning a state or zip to a territory.
Creation and Management
To create a Custom Metadata Type, navigate through Setup and click New. Similar to the creation of a custom object, you are prompted to add a name and plural name, API name, description and visibility. From there, you can create the custom fields required to meet your use case.
Adding and editing records is handled declaratively by clicking ‘Manage Records’. Custom Metadata Types can also have relationships by creating a EntityDefinition.
Accessing Metadata Records
There are several ways to use the data in a Metadata record. For developers, Apex can access the data as long as the metadata records are visible in the code’s namespace. The syntax looks like this:
Queries can be run and the returned data can be used for calculations, decisions, etc.
For admins, the metadata records can be accessed via validation rules, formula fields and flows.
Real World Example
Here is a real example of how I have used Custom Metadata Types to solve a client problem.
A company sells through a channel, and their partners are ranked based on sales volume. The sales thresholds are geographically unique, and need to be updated a few times per year. A metadata type was created for holding this information. The Partner_Tier__mdt type was created, and fields were added for Bronze Tier, Silver Tier, Gold Tier, and Platinum Tier. This contained the threshold, associated pricebook, and certified employees for each tier.
Once created, metadata records were created for the individual territories (US, Canada, UK, etc.). On the Account object, custom fields were created.
- Partner Tier Reference – Picklist containing location (US Partner, UK Partner, etc.). This maps to the metadata record that will be used for calculation.
- Partner Tier – Formula field to compare sales to thresholds in metadata record. In this case, I use CASE syntax with the following metadata reference:
- Sales to Next Tier – Formula subtracting current sales from next tier using CASE statement.
A trigger is used to assign the pricebook to all opportunities based on the partner’s tier. The benefits to this setup is that all partner tier calculations happen automatically when the corresponding tier metadata record is updated. The company can work on a new set of tier records in a sandbox and deploy to production when ready.
Custom Metadata Types are an interesting option to solve problems. If you are unfamiliar with them, I recommend doing this Trailhead for some background. If you have an interesting use case, feel free to share in the comments below.