Custom Metadata Types

Custom metadata is customizable, deployable, packageable, and upgradeable application metadata. First, you create a custom metadata type, which defines the form of the application metadata. Then you build reusable functionality that determines the behavior based on metadata of that type. Similar to a custom object or custom setting, a custom metadata type has a list of custom fields that represent aspects of the metadata. After you create a public custom metadata type, you or others can declaratively create custom metadata records that are defined by that type. When you package a public custom metadata type, customers who install the package can add their own records to the metadata type. Your reusable functionality reads your custom metadata and uses it to produce customized application behavior.

Custom metadata rows resemble custom object rows in structure. You create, edit, and delete custom metadata rows in the Metadata API or in Setup. Because the records are metadata, you can migrate them using packages or Metadata API tools. Custom metadata records are read-only in Apex and in the Enterprise and Partner APIs.

Custom Metadata Types are similar to Custom Settings. Let’s see an example to use it in Salesforce.

1. Go to Custom Metadata Types.

2. Click “New Custom Metadata Type”.

3. Save Custom Metadata Type.

4. Click “New” in Custom Fields section.

5. I have created a Field with API name Name__c.

6. Click Manage to create records.

7. Click “New” to create records.

8. Save a test record.

9. To see the records in a VF page use the below code

Visualforce page:

<apex:page controller=”Sample”>
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value=”{!listEmpSettings}” var=”e”>
            <apex:column value=”{!e.DeveloperName}”/>
            <apex:column value=”{!e.Name__c}”/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>


Apex Controller:

public class Sample {
    public List<Employee_Setting__mdt> listEmpSettings {get;set;}
    public Sample() {
        listEmpSettings = [SELECT DeveloperName, Name__c FROM Employee_Setting__mdt];
    }
}

Output:

Cheers!!!

Leave a Reply