Using Custom Metadata Type in Salesforce

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) .

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).

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.

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

Now lets create the Visualforce page.

[sourcecode language=”java”]&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;
[/sourcecode]

Here is the attached screenshot of the Visualforce page

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

Salesforce CRM Insights
From Data to Action: Tableau Agent’s Role in Shaping Salesforce CRM Insights
future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top