Uncategorized

Using Custom Metadata Type in Salesforce

Custom Metadata Types have great and valuable features which makes it very useful package-able component. It can be used as custom setting  and can be deployed as any other metatdata type.

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 (that’s the only limitation which I feel makes where you might need to choose Custom setting over custom metadata type).

The biggest advantage Custom metadata type gives is it does not count into SOQL queries for each Apex transaction.You can go through the details of Custom Metadata Type in Salesforce Documentation (https://help.salesforce.com/HTViewHelpDoc?id=custommetadatatypes_overview.htm). You can also go through the implementation guide of Custom Metadata Type (http://resources.docs.salesforce.com/200/9/en-us/sfdc/pdf/custom_metadata_types_impl_guide.pdf).

In this article I will try to explain how we can get the country names based on the country codes in visualforce pages. A very common scenarios where developers generally use list type Custom Setting to store the mapping of Country Codes and Country Names.

We will create a Custom Metadata Type called CountrySetting with a custom field called Country Name. Attached Screenshot is the configuration of the Custom Metadata Type.The Custom Metadata Type has a suffix of __mdt instead of __c (for Custom objects/fields). We can also create a picklist field in custom metadata type (Currently this is in beta phase and hopefully will be available globally soon) .

Screen Shot 2016-02-23 at 9.20.46 pm

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

Screen Shot 2016-02-23 at 10.04.15 pm

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

I have created an apex class which has a method which returns a list of custom metadata types.

public with sharing class CountrySettingController {
public list<CountySetting__mdt> getCountrySetting(){
return [Select DeveloperName,MasterLabel, Country_Name__c from CountySetting__mdt];
}
}

Now lets create the Visualforce page.

&lt;/pre&gt;
&lt;pre class=&quot;codeBlock&quot;&gt;&lt;apex:page controller=&quot;CountrySettingController&quot;&gt;
  &lt;apex:sectionHeader title=&quot;Custom Settings&quot; subtitle=&quot;List Demo&quot;/&gt;
  &lt;apex:form &gt;
    &lt;apex:pageBlock &gt;
    &lt;apex:pageBlockTable value=&quot;{!CountrySetting}&quot; var=&quot;item&quot;&gt;
            &lt;apex:column value=&quot;{!item.MasterLabel}&quot;/&gt;
            &lt;apex:column value=&quot;{!item.DeveloperName}&quot;/&gt;
            &lt;apex:column value=&quot;{!item.Country_Name__c}&quot;/&gt;
        &lt;/apex:pageBlockTable&gt;
    &lt;/apex:pageBlock&gt;
  &lt;/apex:form&gt;
&lt;/apex:page&gt;

Here is the attached screenshot of the Visualforce page

Screen Shot 2016-02-23 at 10.19.35 pm

If you check the log files , you can see that the method which queries the Custom metadata type does not count towards the SOQL Limit. This was a basic example which shows how we can use Custom metadata type in apex.Since custom metadata types are metadata rather than data, they leverage all the tools Force.com already has for managing, packaging and deploying metadata. This makes them very useful if you are building package-able apps.

Advertisements

3 thoughts on “Using Custom Metadata Type in Salesforce

  1. This is interesting but the feature seems to be missing something … to wit:

    How do you validate that the rows of custom metadata pass your “business rules”. There are no VRs on __mdt, you can’t write triggers on __mdt, and the only thing I could think of was to create a bespoke VF page whose controller did validation in APEX before saving the rows via the APEX metadata API wrapper.

    Using your example, verifying no duplicates; verifying that country codes were two character alpha; verifying that two country codes don’t map to the same country name, etc.

    Liked by 1 person

    1. Yes you are right but the idea of using custom metadata type is to use for packaging. That way you can send the records as part of the package. Custom Setting had that limitation and Custom Metadata type overcomes that

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s