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.
Custom Metadata Types in Salesforce are similar to the custom objects. It has a suffix of “__mdt” instead of “__c” in the API namespace. Let’s say if you create a Custom Metadata type with name “Salesforce_funda”, the API name of the metadata type would appear as – “Salesforce_funda__mdt”
It allow you to build dynamic applications using Apex on the Salesforce Platform and also enables you to have more control over the execution of your code.
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. For example, you can use custom metadata types for these use cases
- Structure: Custom Metadata Types define the structure and fields of the custom metadata records. Each custom metadata type has a set of fields that you define, similar to custom objects. You can specify field types, relationships, and other properties.
- Deployment: Custom Metadata Types are deployed as part of the Metadata API, allowing you to package and deploy them along with other components of your Salesforce application. This makes it easy to move metadata records between different environments and organizations.
- Record-based Configuration: Custom metadata records can be used to configure various aspects of your Salesforce application. For example, you can define custom settings, feature flags, or mapping tables using custom metadata records. These records can be accessed and utilized by your application's code and configuration.
- Global Visibility: Custom metadata records have a global visibility, meaning they can be accessed and used across the entire Salesforce org. This makes them particularly useful for creating reusable configuration data or settings that need to be shared across different parts of your application.
- No DML Operations: Unlike custom objects, you cannot perform DML (Data Manipulation Language) operations like create, update, or delete on custom metadata records at runtime. Custom metadata records are read-only at runtime, and any changes to their values should be made through the Metadata API or through the Salesforce Setup menu.
- Apex Access: Custom metadata records can be accessed and manipulated in Apex code using SOQL (Salesforce Object Query Language) queries. This allows you to dynamically retrieve and utilize the configuration data stored in custom metadata records within your application's logic.
- Secrets: Store information, such as API keys, in your protected custom metadata types within a package.
Custom metadata rows resemble custom object rows in structure. You create, edit, and delete custom metadata rows in Metadata API or in Setup. Because the records are metadata, you can migrate them using packages or Metadata API tools.
Custom Metadata In Apex :-
Custom metadata types methods are instance type methods and are called by and operate on a specific instance of a custom metadata type.
The following methods are available in Apex to fetch it:
- getAll() :- Returns a map containing custom metadata records for the specific custom metadata type. The map keys are the record DeveloperNames and the map values are the record sObjects.
- getInstance(recordId) :- Returns a single custom metadata type record sObject for a specified record ID.
- getInstance(developerName) :- Returns a single custom metadata type record sObject for a specified developerName field of the custom metadata type object.
- getInstance(qualifiedApiName) :- Returns a single custom metadata type record sObject for a qualified API name.
How to Create Custom Metadata Type :-
1 ) :- go to the Setup, Quick Find Box, Search For Custom Metadata Type
2 ) :- Click and Open it and Click on New Custom Metadata Type Button
3 ) :- Now fill the Following details
Label :- Country Codes
Plural Label :- Country Codes
Object Name :- Country_codes
Description :- accordinly we can give something
Visibility :- All Apex code and APIs can use the type, and it's visible in Setup.
click on save
4 ) :- Now you will see new screen like this create new custom fields by clicking in New button in Custom Field section
5 ) :- Select the field type for the field you want to create it support many types, we can see it in below screenshot
6 ) :- Select Text Type and Click on Next and Fill the Below details
Field Label :- Country Short Code
Length :- 5
Field Name :- Country_Short_code
Description :- accordingly we can give something
Click on Next, Then Save
7 ) :- Click on New Button
8 ) :- Click on New Button and Create Records like this, Click on Save or if again we need to create click on Save & New
To access the Custom Metadata Type using SOQL
//to get all the fileds from metadata type
Select FIELDS(ALL) From Country_codes__mdt LIMIT 200
//to get the some fields giving the name of the fields
Select Label, Country_Short_Code__c From Country_codes__mdt
To access the Custom Metadata Type Using Apex and SOQL in Apex
//Values using SOQL
List<Country_codes__mdt> countrycodeList = [Select Label, Country_Short_Code__c From Country_codes__mdt];
for(Country_codes__mdt ccm : countrycodeList){
System.debug('ccm'+ccm);
}
//getALl.values() as List
List<Country_codes__mdt> countrycode = Country_codes__mdt.getAll().Values();
System.debug('countrycodeList------'+countrycode);
for(Country_codes__mdt ccm : countrycode){
System.debug('ccm label '+ccm.label+' ccm Country_Short_Code__c '+ccm.Country_Short_Code__c);
}
//getAll values in Map
Map<String,Country_codes__mdt> mapCountryCodes = Country_codes__mdt.getAll();
for(string ccm : mapCountryCodes.keyset()){
System.debug(' ccm Country_Short_Code__c '+mapCountryCodes.get(ccm).Country_Short_Code__c);
}
//getInstance(recordId)
Country_codes__mdt CountryCodeRecord = Country_codes__mdt.getInstance('m015g0000017K73AAE');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Short_Code__c);
//getInstance(developerName)
Country_codes__mdt CountryCodeRecord = Country_codes__mdt.getInstance('USA');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Short_Code__c);
//getInstance(qualifiedApiName)
Country_codes__mdt CountryCodeRecord = Country_codes__mdt.getInstance('England');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Short_Code__c);Benefits :-
- Using Apex getAll(), getInstance(recordId), getInstance (qualifiedApiName), and getInstance(developerName) methods are used to retrieve information from custom metadata type records faster. These methods don’t rely on the SOQL engine and return the sObject details directly by the call. Below are few benefits of these methods.
- It removes need of Salesforce Object Query Language (SOQL).
- Eliminate any SOQL limits.
- Build the code cleaner and faster.










