Custom Meta Data Types

Published on
July 28, 2016

While deploying any functionality or application to another org either by using change sets or packages we can deploy only Meta data like Custom Objects, Apex Classes, Visualforce Pages and Custom Settings but the record data like instances of Custom Objects and Custom Settings cannot be included.

After installing the package we need to also undertake some post install activities like configuring master data for the application.

By using Custom Metadata Types we can migrate configuration data (records) from one org to another org very easily at the time of deployment by using packages or Metadata API tools. It will definitely reduce the amount of effort required to populate records required by an application.

Custom Metadata Types are similar to our salesforce objects or custom settings.

Custom metadata types support the following custom field types.

• Checkbox

• Date

• Date and Time

• Email

• Number

• Percent

• Phone

• Picklist

• Text

• Text Area

• URL

It has a list of custom fields to hold Meta data records in addition to records which object and custom settings are holding.

The Custom Metadata records can be enquired about just like normal records using SOQL and it does not count into SOQL queries for each Apex transaction.

The Custom Metadata Type has a suffix of __mdt instead of __c (for Custom objects/fields).

Here I will try to explain how we can get the country names based on the country codes in visualforce pages. Below is the scenario where every developer generally uses list type Custom Setting to store the mapping of Country Codes and Country Names.

Develop ->Custom Metadata Types -> New Custom Metadata Type

1

Visibility means who will be able to see the type:

• Public—anyone can see it.

• Protected—if the type is installed as part of a managed package, only Apex code in that managed

package can use it

2

Create a field called Country Name of type Text

3

Let us create some data so that it displays in our Visualforce page. You can click on Manage button and further click on New to create the records (Similar to creating records in Custom setting).

4

Next step is to build a Visualforce page which shows the Country Codes and Country Names and the controller required to build that.

Apex Class

public with sharing class MetaDatatypeDemoCC {

public list<Country_Settings__mdt>getCountrySetting(){

return [Select DeveloperName,MasterLabel, Country_Name__c from Country_Settings__mdt];

}

}

Visual Force Page

<apex:page controller="MetaDatatypeDemoCC">

<apex:pageblock>

<apex:pageblockTable value="{!CountrySettings}" var="country">

<apex:column value="{!country.MasterLabel}"/>

<apex:column value="{!country.DeveloperName}"/>

<apex:column value="{!country.Country_Name__c}"/>

</apex:pageblockTable>

</apex:pageblock>

</apex:page>Here is the screenshot of the Visualforce page

5

By checking log files the method which queries the Custom Metadata Type does not count towards the governor limits. Since custom metadata types are metadata rather than data, they leverage all the tools Force.com already has for managing, packaging and deploying metadata. It is extremely useful if you are building package-able apps.

Setup a consultation