In Salesforce Creating Custom Metadata Type is similar as creating custom objects or custom settings. The records of a custom metadata type are themselves metadata, not data. Custom Metadata Type and it is records can be moved from one org to another org during deployment, unlike Custom Settings where only metadata is eligible for migration. Typically, Custom Metadata type is deployable, packageable, customizable, and upgradeable. The main advantage of Custom Metadata type is that it does not count towards SOQL queries limits for each APEX transaction.
To create a Custom Metadata Type:
Go to[Symbol]Setup[Symbol]Develop [Symbol]Custom Metadata Types[Symbol] New Custom Metadata Type.
Custom Metadata Type Visibility:
- All Apex code and APIs can use the type, and it is visible in Setup (public):- As the name suggests, Custom metadata type is available in public where anyone can see it and all apex code can access this custom metadata type.
- Only Apex code in the same managed package can see the type (Protected):- Custom metadata type is available internally and only the same managed package can access this custom metadata type.
Creating and managing fields on Custom Metadata Type:
Creating field under Custom Metadata Type is same like creating fields under custom objects but with extra field called Metadata Relationship and security option called Field Manageability.
While creating fields under Custom Metadata Type, field manageability is used to manage custom fields. The following custom field types are supported by Custom metadata type:
- Metadata Relationship
- Date and Time
- Text Area
Below are the options used to manage the fields:
- Only package developer (via package upgrade): – The created field can be edited by the package developer only after the deployment using package upgrade and it cannot be edited in the org in which it is deployed.
- Any user with Customize Application permission (package upgrades won’t override the value): – The created field can be edited in the org in which it is deployed but cannot be overridden using the package upgrade.
- No one: – Once the package is released even the package developer and the org in which the package is deployed cannot edit the field.
Metadata Relationship Field:
Metadata Relationship field is an additional feature available in custom metadata type but not in custom setting. Creating this field is same as creating Lookup field in the custom object. While creating records under Custom Metadata Type, we can select the Parent object in the lookup i.e. another Custom Metadata Type record to which the child object (Custom Metadata Type) is related to. Querying custom metadata relationships is same as we query the normal relationships.
The following are the considerations for the Metadata Relationship field:
- Public custom metadata types cannot be related to protected custom metadata types, but protected custom metadata types can be related to Public custom metadata types.
- Public custom metadata records cannot be related to protected custom metadata records, but protected custom metadata records can be related to Public custom metadata records.
Creating records under Custom Metadata Type:
Creating records under Custom Metadata Type is same as creating in custom objects. Here, Actual Value Setting is an example of Custom Metadata Type. In the detail page of the Custom Metadata Type, there is a button called “Manage Actual Value Settings”. By clicking this button, it will navigate to the corresponding record creation page with specific Custom Metadata Type.
After clicking “Manage Actual Value Settings”, click “New” to go to the below page where “Protected Component” check box is available in record edit page. By selecting the Protected Component checkbox, we can control the visibility of individual records. A protected record can be accessible only in its own namespace, whereas public record is available to the entire org.
In the above edit page, while creating records two fields will be available by default and as a mandatory– Label used to store the name of the record and Actual Value Setting Name (Custom Metadata Record Name) which stores the unique name of the record used by the API.
Querying Custom Metadata Type records:
Querying records created under Custom Metadata Type and querying records of metadata relationship field is same as querying records from objects with one difference where custom metadata type has an API which ends with ”__mdt”. The maximum amount of SOQL queries per Apex transaction is unlimited. Below is the example query accessing from child to parent:
Parent object API Name: Actual_Value__mdt
Child object API Name: Actual_Value_Setting__mdt
Metadata Relationship API field: Actual_Value__c , which relates to Actual_Value__mdt (Parent object).
Relationship Query (Child to Parent):
SELECT ID, Custom_Value__c, Phone__c, Actual_Value__r.MasterLabel, Value_Rating__c, DeveloperName FROM Actual_Value_Setting__mdt
Deployment of Custom Metadata Type:
Adding custom metadata type in the change set is like adding metadata components in the change set but with one difference where we can select the component as custom metadata type name and we can add its associated records as well in the change set to deploy. This is the core feature of the Custom Metadata Type.
Initially in the change set Custom Metadata Type should be added and its associated records which need to deploy can be added as shown in the below screenshot.
The following are the limitations of Custom Metadata Type:
- Custom metadata records cannot exceed the size of 10MB
- It does not support formula field data type.
- Can create only 100 fields per custom metadata type.
- Global picklists are not supported.
- Only 100 Custom Metadata Type can be created per Organization. This number includes all types developed in the organization and installed from managed and unmanaged packages.