Custom Metadata Types are a powerful feature in Salesforce that allows developers to store metadata in a flexible, scalable way. They are essentially custom objects that use to store metadata. Custom Metadata Types make it easy to package and deploy this data across different environments, such as sandboxes and production or
They are stored in their own custom object, separate from the thing they are used to customize, and can be accessed by Apex code, Visualforce pages, and Lightning components.
Creating a Custom Metadata Type:
To create a Custom Metadata Type, go to Setup > Custom Metadata Types > “New Custom Metadata Type”. You’ll need to give your Custom Metadata Type a label, name, and description, just like you would for a regular custom object.
Step 1: First, go to Setup and search for “Custom Metadata Types”. Click “New Custom Metadata Type” as shown below.
Step 2: Fill in the required fields, including the label and the API name. You’ll also need to choose a data type for your records. The available data types include checkbox, ****, email, number, percent, picklist, and text.
Step 3: Create custom fields. Click the ‘New’ button as shown below. fill in the Labels and save.
Step 4: After creating fields, you can proceed to create records for it. To accomplish this, navigate to ‘Manage Metadata Name’ button and then click ‘new’.
Step 5: Enter fields in the records. The fields will be based on the data type you chose when creating the Custom Metadata Type. For example, if you chose “text”, you’ll be able to enter text values for your fields.
Step 6: Save your record.
Here’s an example of what a Custom Metadata Type record might look like:
Label: Application Settings
API Name: Application_Settings__mdt
Field Name: Setting Name
Data Type: Text
Field Name: Setting Value
Data Type: Text
Record 1: Setting Name: Default Language
Setting Value: English
Record 2: Setting Name: Max Users
Setting Value: 100
Once you’ve created records for your Custom Metadata Type, you can access them in your Apex code using SOQL queries.
Here’s an example:
List < Application_Settings__mdt > settings =[SELECT Setting_Name__c, Setting_Value__c FROM Application_Settings__mdt]; //iterating custom metada types for (Application_Settings__mdt setting : settings) { //checking fields if (setting.Setting_Name__c == 'Default Language') { String defaultLanguage = setting.Setting_Value__c; System.debug('Default Language: ' + defaultLanguage); } //checking fields else if (setting.Setting_Name__c == 'Max Users') { Integer maxUsers = Integer.valueOf(setting.Setting_Value__c); System.debug('Max Users: ' + maxUsers); } }
In this example, we’re retrieving all records from our Custom Metadata Type and then iterating over them to find the values of the “Default Language” and “Max Users” settings. We’re then printing out the values of those settings.
Advantages of Custom Metadata Types in Salesforce :
- Flexible data model: With Custom Metadata Types, you can define a data model that is tailored to the needs of your application. This allows you to create fields that are specific to your use case and establish relationships between records. As a result, your application can be more flexible and better suited to your needs.
- Dynamic data: Unlike Custom Objects, Custom Metadata Types can be updated without the need for code changes or recompiling. This allows you to update data on the fly and without having to go through the full development lifecycle.
- Easy to manage: Custom Metadata Types can be managed through the Salesforce Setup interface, making it easy for administrators to create, modify, and delete records. Additionally, you can use Salesforce’s built-in tools to validate your Custom Metadata Type data and ensure that it’s accurate and up-to-****.
- Better scalability: Custom Metadata Types provide better scalability for larger data sets. Because it stores the data in Salesforce’s metadata layer, it doesn’t count against the governor limits that apply to other types of data in Salesforce.
- Easy to deploy: Custom Metadata Types are easy to deploy across different Salesforce organizations.
- Integration with Apex code: Custom Metadata Types are easily accessible and queried from Apex code, making it easy to integrate with other Salesforce functionality.
Conclusion:
In conclusion, Custom Metadata Types are a powerful feature in Salesforce that allows you to store custom metadata in a flexible, scalable way. They make it easy to package and deploy metadata across different environments and are accessible by Apex code, Visualforce pages, and Lightning components. By using Custom Metadata Types, you can build more dynamic and customizable Salesforce applications that are easier to manage and maintain.