Custom Metadata Types offer Salesforce administrators and developers an invaluable tool for configuring and managing custom metadata records. Unlike custom objects, it enable the seamless deployment of metadata across multiple Salesforce orgs. In this blog post, we will delve into the ins and outs of working with custom metadata types, exploring their benefits and providing step-by-step guidance for implementing them effectively.
Prior to the Summer ’15 release, the Salesforce Developer Community had limited capabilities when it came to creating metadata. Developers could only create metadata for the objects that Salesforce provided. The introduction has empowered both admins and developers to actively create and deploy their own configuration. Within the custom metadata types, they can customize and update the metadata as needed, providing greater flexibility and control. This significant enhancement allows for the creation and management of custom metadata types by admins and developers, enabling them to configure applications beyond the predefined options offered by Salesforce.
Salesforce recommends using this functionality for the following use cases:
- Mappings: Create associations between different objects by using custom metadata types. For example, you can assign cities, states, or provinces to specific regions in a country.
- Business Rules: Combine configuration records with custom functionality. You can utilize custom metadata types along with Apex code to route payments to the correct endpoint.
- Master Data: Define custom charges, such as duties and VAT rates, by creating a custom metadata type. Including this type as part of an extension package allows subscriber orgs to reference the master data.
- Whitelists: Manage lists, such as approved donors and pre-approved vendors, using custom metadata types.
- Secrets: Safely store information like API keys within protected custom metadata types in a package.
To create a custom metadata type in Salesforce, follow these steps:
Before embarking on the process of creating a custom metadata type, it is important to ensure that you have the necessary permissions. Typically, administrators should be the ones responsible for creating these records. Hence, to enable programmatic access, you need to have either the “Customize Application” or “Author Apex” permissions.
Furthermore, it is important to consider the permissions for other users who may need to view these records. This can be managed effectively by assigning appropriate profiles or, even better, using permission sets specifically tailored for each custom metadata type. By leveraging profiles or permission sets, you can control the visibility of custom metadata records for different users in a more granular manner.
Define A Custom Metadata Type Declaratively
Creating a custom metadata type is a straightforward process once you opt for the custom metadata approach instead of creating multiple fields on different objects to store specific information.
Upon navigating to the “Custom Metadata Types” section and clicking the “New Custom Metadata Type” button, you will be prompted to provide the name, description, and determine the visibility settings. Additionally, you can choose to make it visible in subscriber orgs or restrict it for internal use only.
To create all the custom fields, so let’s get started
Although creating the custom metadata type was straightforward, you might find creating the fields challenging depending on your use case – just like when working with any other Salesforce object in the past.
Since the example of the custom metadata type involves storing the Country Code for each Country, I have decided to create a custom field called Country Code. In this particular case, I will use the Country’s name as the label. It is important to note that the custom field has the suffix “__c,” while the custom metadata type itself has the suffix “__mdt,” unlike any other object.
The similarities to a custom or standard Salesforce object do not stop there. You can also create list views, validation rules, and page layouts. After creating the new Country Code field, I proceeded to edit the layout and designated the field as required.
You have the ability to create various field types on a custom object, including picklist, number, and text. Additionally, although a master-detail relationship field is not available, you can utilize the Metadata Relationship field type. This opens up numerous possibilities by establishing a relationship with another custom metadata type or entity definition.
Engage in practical learning of metadata relationship fields by completing this dedicated Trailhead module!
Add or Edit Custom Metadata Records Declaratively
Now that you have the foundation of the ‘object’, proceed to create records. Once again, you need to search for “Custom Metadata Types” in Setup.
However, this time, as the custom metadata type already exists, click on “Manage Records” next to the one for which you intend to modify the records.
The status of the “Protected Component” checkbox determines whether the record is protected or not. A protected record can only be accessed by code within the same namespace as the record itself or its associated custom metadata type. This includes the code you create, code in an unmanaged package, and code in the same managed package or its custom metadata type. Once you have completed the modifications, click on “Save”
Mass Insert or Update Custom Metadata
Creating or updating multiple custom metadata records simultaneously can be a challenging task, especially when it comes to doing it declaratively in Setup as it is provided by default. As you can imagine, manually modifying or creating numerous records can quickly become a tedious and time-consuming endeavor.
However, despite the Custom Metadata Loader being available for quite some time now, it remains an efficient method for importing multiple records from a CSV file into your Salesforce org.
This approach is particularly favorable for administrators since access to this functionality is regulated through a permission set, allowing them to create or update custom metadata type records effortlessly. Admins can swiftly update dozens of records directly in their sandbox, streamlining the process.
You can get the Custom Metadata Loader along with the instructions here.
Starting from Summer ’20, CLI commands widely accessible for developers to manipulate custom metadata type records. Furthermore, records can directly created from a CSV file. Ensure to review all available commands and examples by referring to the provided resource.
Deploy Custom Metadata Types
One advantage of custom metadata types is their complete deployability and packagability. They can be effortlessly deployed using change sets, a functionality commonly utilized by admins to move components between environments. Alternatively, you can retrieve and deploy them directly to the desired org using Metadata API.
Let’s see how the records we just created look like as XML files when exploring them using the brand new Code Builder.
Note: If needed, just as you would in VSCode, you can edit the files, save your changes, or when it makes sense – overwrite them with the metadata in your org.
Advantages of Custom Metadata Types
Now that you have gained a clear understanding of how you can potentially work with custom metadata types and manage them, let’s explore some of the advantages of utilizing this functionality.
Supports Complex Data Types:
It is crucial to emphasize the significance of this capability – you have the freedom to design your custom metadata types as simple or as complex as you desire. You can create fields directly on the custom metadata type to store your data. Admins can effectively manage the records, while users also have access to them if necessary.
The following field types can be created:
- Metadata Relationship
- Date and Date/Time
- Email and Phone
- Text and Text Area
Considerations of Using Custom Metadata
- 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.
- Custom metadata types are not supported in muting permission set .
- Why custom metadata is used in Salesforce?
In short, custom metadata is used in Salesforce for configurable application data, dynamic and scalable configurations, centralized and reusable settings, packaging and deployment, and support for complex data types.
2.What is the difference between custom data and custom metadata in Salesforce?
Custom data refers to user-inputted records and information, while custom metadata is used for application configuration settings and metadata. Custom data is visible and accessible to users, while custom metadata is primarily accessed by administrators and developers for configuration purposes.
3. Why use custom metadata instead of custom object?
Custom Objects records only take 2kb of space, no matter how much content there is. However, custom metadata size is decided on basis of field size & not actual records. So, if you have a text field of size 255 characters and storing only 10 characters, Salesforce would still count it as 255 characters.
4. What is the difference between metadata and custom metadata?
In essence, metadata encompasses the overall components and configurations, while custom metadata, on the other hand, specifically focuses on creating custom application configurations and settings.
5. How do I query custom metadata?
- while injecting parameter values into variables to be injected in the WHERE clause, use “:” colon before it.
- place any WHERE clause in custom metadata type query as appropriate.
- Apex has visibility to custom metadata type.