Introduction to Custom Metadata Types in Salesforce

Introduction to Custom Metadata Types in Salesforce

Introduction to Custom Metadata Types in Salesforce: Empowering Scalable and Customizable Solutions

Prior to the Summer '15 release, the Salesforce Developer Community had limited capabilities to create custom metadata for objects outside of Salesforce's provided options. This meant that admins and developers could only configure applications using the predefined features.

However, with the introduction of custom metadata types, both admins and developers gained the ability to create and deploy their own configuration and application metadata. This opened up new possibilities for customization and flexibility.

Salesforce recommends utilizing custom metadata types for various use cases, including:

  1. Mappings: Creating associations between different objects, such as assigning cities, states, or provinces to specific regions within a country using custom metadata types.

  2. Business Rules: Combining configuration records with custom functionality, such as routing payments to the correct endpoint using custom metadata types and Apex code.

  3. Master Data: Defining custom charges, like duties and VAT rates, through custom metadata types. By including this type in an extension package, subscriber orgs can reference the master data.

  4. Whitelists: Managing lists, such as approved donors or pre-approved vendors, using custom metadata types.

  5. Secrets: Storing sensitive information, such as API keys, securely within protected custom metadata types included in a package.

By leveraging custom metadata types, admins and developers can enhance their Salesforce configurations, tailor applications to specific needs, and effectively manage important data and rules.

Steps for Creating Custom Metadata Type

Before embarking on the process of creating a custom metadata type, it is important to ensure that you have the necessary permissions. Typically, administrators are responsible for creating these records. Therefore, they should have either the "Customize Application" or "Author Apex" permissions to enable programmatic access.

Furthermore, it is often necessary for other users to have permissions to view these records, depending on the specific use case. This can be managed by assigning appropriate profiles or, even better, by using permission sets for each custom metadata type. By properly configuring permissions, you can control who can access and view the custom metadata records, ensuring the right level of visibility for different users.

Define A Custom Metadata Type Declaratively

Once you opt for utilizing custom metadata instead of managing information through multiple fields on different objects, the process of creating custom metadata becomes straightforward.

To begin, you can navigate to the "Custom Metadata Types" section and click on the "New Custom Metadata Type" button. This action will prompt you to provide the name and description for the custom metadata type, as well as make decisions regarding the visibility settings.

Create All Custom Fields You Need

While the process of creating a custom metadata type is straightforward, configuring the fields can be a bit more challenging depending on your specific use case. It is similar to working with any other Salesforce object you may have encountered in the past.

In my example of a custom metadata type for storing Country Codes, I decided to create a custom field called "Country Code." However, in this case, I used the label of the field to represent the name of each country. It's important to note that the custom field has the suffix "__c," while the custom metadata type itself has the suffix "__mdt," distinguishing it from other objects in Salesforce.

The similarities to a custom or standard Salesforce object don't end here. Just like with other objects, you can create list views, validation rules, and page layouts for custom metadata types. After creating the new Country Code field, I also edited the layout and made the field a required field.

In addition to being able to create field types such as picklist, number, and text, there is another field type available for custom metadata types called Metadata Relationship. While a master-detail relationship field is not an option, the Metadata Relationship field type allows you to establish relationships with other custom metadata types or entity definitions, opening up a range of possibilities.

Get hands-on with metadata relationship fields by going through this dedicated Trailhead module!

Add or Edit Custom Metadata Records Declaratively

Now that you have set up the foundation of the custom metadata type, it's time to create some records. To do this, search for "Custom Metadata Types" in Setup once again.

However, this time, since the custom metadata type already exists, click on "Manage Records" next to the specific custom metadata type for which you want to modify or create records.

The "Protected Component" checkbox controls the accessibility of the record. When the checkbox is selected, the record is considered protected. This means that it can only be accessed by code within the same namespace as either the record or its associated custom metadata type. This includes the code you create, code in an unmanaged package, and code in the same managed package as either the protected record or its custom metadata type. After making the necessary changes, click on "Save" to save the record.

Mass Insert or Update Custom Metadata

Managing multiple custom metadata records can be a challenging task, especially when it comes to manual creation or modification. However, Salesforce provides a solution called the Custom Metadata Loader, which allows you to import multiple records from a CSV file into your Salesforce org.

The Custom Metadata Loader has been available for a while and offers a fast and efficient way to import records. It is particularly convenient for admins, as they can control access to this functionality through permission sets. With the Custom Metadata Loader, admins can quickly create or update multiple custom metadata type records, saving time and effort.

This tool empowers admins to make changes to dozens of records within minutes, directly in their sandbox environment. It simplifies the process of managing custom metadata records and enhances administrative productivity.

You can get the Custom Metadata Loader along with the instructions here.

Starting from the Summer '20 release, CLI commands for manipulating custom metadata type records have become generally available, providing developers with a simpler way to create new records or fields. With the CLI commands, developers can efficiently manage custom metadata type records directly from the command line interface.

One notable feature is the ability to create records from a CSV file, eliminating the need for manual record creation unless only a few new records are required. This feature streamlines the process by allowing developers to import records in bulk, saving time and effort.

To leverage the CLI commands for custom metadata type records, developers should explore the available commands and refer to the provided examples for a better understanding of their usage. The CLI commands offer a convenient and efficient approach for developers to handle custom metadata type records in their Salesforce development workflow.

Source:help.salesforce.com

Deploy Custom Metadata Types

Custom metadata types offer the benefit of being completely deployable and packageable. This means that they can be easily deployed using change sets, which is a useful feature for administrators who need to move components between different environments. Alternatively, you can directly retrieve and deploy custom metadata types to the desired organization using the Metadata API.

Let's examine the XML representation of the records we recently created by utilizing the newly introduced Code Builder.

Please note that, similar to working with VSCode, you have the ability to edit the files, save any modifications, and if appropriate, overwrite them with the metadata present in your organization.

Advantages of Custom Metadata Types

Now that you have familiarized yourself with the process of working with custom metadata types and how to manage them effectively, let's explore some of the benefits of utilizing this functionality.

Supports Complex Data Types

As mentioned earlier, it is crucial to emphasize the significance of the flexibility provided by custom metadata types. You have the freedom to create fields directly on the custom metadata type, allowing you to design solutions that range from simple to complex. This enables administrators to manage the records while also granting access to users when necessary.

The available field types that can be created include:

  • Metadata Relationship

  • Checkbox

  • Date and Date/Time

  • Email and Phone

  • Number

  • Percent

  • Picklist

  • Text and Text Area

  • URL

With this range of field types, you have the versatility to tailor your custom metadata types to suit your specific requirements.

Can Be Referenced in Formula Fields, Validation Rules, Apex, and Flows

As you may already be aware or have inferred, the information stored within the custom metadata type can be utilized across various processes throughout your entire Salesforce organization. Whether it involves declarative functionality like validation rules or Apex classes, the records are readily available for use.

In the provided example, a custom metadata type called "Sales Engineers" was created to manually add users to Account Teams based on an Excel spreadsheet. Currently, there is one sales engineer defined per Industry, ensuring that each Industry has an assigned sales engineer. However, it is important to note that additional criteria may arise in the future. If necessary, an extra custom field can be created to accommodate these evolving requirements.

Currently, we will utilize the available information, specifically the Industry field, to assign the sales engineer to an account whenever a new account is created and the Industry field is not empty. However, it's important to acknowledge that every organization and requirement may vary, and you might opt for a different approach with alternative entry criteria or additional custom fields on the custom metadata type.

Please note that the tutorials provided are based on Developer Edition orgs with limited data and a few custom automations. It is crucial to thoroughly evaluate your existing processes before implementing new ones, taking into account both declarative automations and custom Apex. When working with Flows, consider following the recommended best practices, including the appropriate number of record-triggered flows per object.

Make Changes in One Place

And now, we come to the most compelling aspect of custom metadata types: the elimination of the need to manage data across multiple Salesforce objects and the avoidance of hard-coding IDs. This is a significant advantage! Furthermore, custom metadata types allow for the creation of structured and organized information within Salesforce, even if it was originally stored outside of the platform.

Moreover, regardless of the process or use case that involves leveraging custom metadata types, their deployability ensures that the entire functionality can be efficiently deployed and tested. This eliminates the need to recreate records in each Salesforce organization, saving valuable time and effort.

Considerations of Using Custom Metadata

In a Salesforce organization, you have the ability to create a maximum of 200 custom metadata types. For each custom metadata type object, you can create up to 100 custom fields.

It's important to note that custom metadata types are not currently supported in muting permission sets. Although this may be a feature of interest, it is not yet available.

Before utilizing custom metadata types, it is essential to review and consider all the other limitations associated with their usage. Familiarizing yourself with these limitations will help ensure that you leverage custom metadata types effectively within your Salesforce implementation.

Summary

If I had to summarize the benefits of custom metadata types in just one word, it would be "scalability." Especially in complex organizations, custom metadata types contribute to delivering robust implementations efficiently, whether as part of a package or a customized solution tailored to specific business needs. This Salesforce functionality enables time-saving, declarative customization of process behavior while maintaining governance over essential data elements.