Metadata Type in Salesforce

What is Metadata ?

Data about data is called metadata i.e Metadata describe data. Whene you are creating a custom field on an Object, you are required to configure few properties such as field name, data type, field length, require or not. This field definition is metadata.

Custom Metadata type extend this features, which is customizable, deployable, packageable and upgradeable. Creating Custom Metadata type and custom metadata type records are almost identical with creating custom object and custom object record. In Apex., custom metadata types can be treated as SObjects and respond to SOQL queries the same way SObject would.

Custom metadata type 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 Supports following Custom field types –

  • Checkbox
  • Date
  • Date and Time
  • Email
  • Number
  • Percent
  • Phone
  • Picklist [Can’t use global picklist values]
  • Text
  • Text Area
  • URL
  • Metadata Relationship
Creating Custom Metadata Type in Salesforce 
 
Try Creating a Custom Setting, you will get a tip like below screen to to use metadata type if you are using list custom setting.

Create Custom Metadata Type

Setup > Develop > Custom Metadata Types

Metadata type Page settings looks similar to Custom Object like below screen –

It also support Page Layouts.

Custom Object Vs Custom Metadata

Custom metadata records can be queried using SOQL, but the platform internally caches the metadata records and can retrieve them more quickly than it retrieves SObject records. Querying custom metadata also doesn’t count towards SOQL limits, which is good news for performance and throughput in enterprise organizations.
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.

Custom Settings Vs Custom Metadata

Custom metadata records can be packaged, which is not the case for either custom objects or custom settings. You have greater control over the visibility of custom metadata types; you can hide the custom metadata types themselves or just hide specific records. You can also control whether or not users can update record values on a field by field basis.
These are major selling points for adopting custom metadata types. Package developers often want to ship default records with their managed packages. Prior to custom metadata types, they would have spent considerable development effort writing post-install scripts to create the data. They would also have to ensure the application coped gracefully with the data not existing; either because it hasn’t yet been created or because a user deleted it.

Custom metadata records can also be pushed from sandboxes to production orgs via change sets. This allows you to follow platform best practices; testing out changes in a sandbox before migrating them via change sets into the production org.

In other hand, records of Custom object and Custom settings can only be migrated manually- For example

  • Export and Import of Data to Salesforce org using Dataloader or Import Wizard .
  • Regenerate the existing data using some script.
  • Renter the whole set of data manually.[Which is practically tedious or impoissible]
All these above manual process are time consuming and error prone.

Difference between Custom Settings and Custom Metadata Type in Salesforce

Functionality Custom Settings Custom Metadata
Unlimited calls/queries
CUD from Apex
Currency type field
Picklist type field
Metadata Relationship
Associate for an organization,
profile, or specific user
◯(Hierarchy)
Metadata deploy Only definitions Definitions and records
Apex tests Needs to create test data Directly access
Translation Workbench Not supported Still not supported


Using Custom Metadata in Apex and VF page

VF Page and Controller –

Output –

 

Access Custom Metadata Records Programatically

Use SOQL to access your custom metadata types and to retrieve the API names of the records of those type. DML operations aren’t allowed on custom metadata in Apex.
The Custom Metadata Type has a suffix of __mdt instead of __c(for custom object and fields) , but the field created in Metadata type are having suffic __c like Custom object.
Following example – query that returns standard and custom fields for all records of ISO2_Code__mdt custom metadata type –

ISO2_CODE__mdt [] codes = [SELECT MasterLabel, Country_Name__c FROM ISO2_Code__mdt];

for(ISO2_Code__mdt c : codes ) {
System.debug(‘Metadata Types values-‘+c.MasterLabel+’ : ‘+
c.Country_Name__c);
}

Limitations of Custom Metadata Types

Caching

Custom metadata records are cached at the type level after the first read request. Caching enhances performance on subsequent requests. Requests that are in flight when metadata is updated don’t get the most recent metadata.

Global Picklist

Global picklists aren’t supported on custom metadata types. You can only use SObject picklists.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s