As we know Salesforce always add new features and based on that there are releases. This maintenance is all about Custom Metadata Type in apex. You no longer need to write a Salesforce Object Query Language (SOQL) to access custom metadata records in Apex.
How: Use the new Apex getAll(), getInstance(recordId), getInstance(qualifiedApiName), or getInstance(developerName) methods to retrieve custom metadata type records.
Descriptions:
getAll()
Returns a map containing custom metadata records for the specific custom metadata type. The map's keys are the IDs (as String) of the records and the map's 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.
Syntax:
MetadataTypeAPIName__mdt variable = MetadataTypeAPIName__mdt.getAll();
For Exam Replace Code after creation of MetaData Type and Record:
Create a Custom Metadata Type:
Label: Country Code
Plural Label: Country Codes
Add a Custom Field:
Data Type: Text
Field Label: Country Code
Length: 3
Enter a Country Code by clicking "Manage Country Codes":
Label: Canada
Country Code: CAN
Create a Class CountryCodeHelper and Paste the below code:
public class CountryCodeHelper {
public static String getCountryCode(String country) {
Country_Code__mdt countryCode = Country_Code__mdt.getInstance(country);
return countryCode.Country_Code__c;
}
}
Comments
Post a Comment