Thursday 3 February 2022

Retrieve Custom Metadata Type Records Using Static Methods

With salesforce Spring21 release , there is no need to write a Salesforce Object Query Language (SOQL) to access Custom Metadata Type in Apex. Salesforce introduces new methods as similar to accessing custom settings. 

Before Spring21 release, To access Custom Metadata Type Record only using SOQL in Apex like below,

// Before Spring 21
List<Country_Code__mdt> listCountryCode = [SELECT Id,Label,MasterLabel,Country_Code__c,DeveloperName from Country_Code__mdt];
      
for(Country_Code__mdt c : listCountryCode){
           System.debug('Label ->'+ c.Label + ',' + 'Country code ->' + c.Country_Code__c + ',' +'Devloper name ->' + c.DeveloperName);
      }
/* OUTPUT
 * Label -> CANADA,Country code ->CAN,Devloper name ->CANADA
 * Label -> INDIA,Country code ->IND,Devloper name ->INDIA
 * Label -> UNITED STATES,Country code ->UNS,Devloper name ->UNITED STATES
*/

Salesforce spring21 release provides an option to access Custom Metadata Type record using static methods without SQOL.

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.
Let's see all methods with example.
  • getAll()
It returns a map containing custom metadata records for the specific custom metadata type. The map's keys are the Record ID and the map’s values are the sObjects record.

List<Country_Code__mdt> listCountryCode = Country_Code__mdt.getAll().values();
      
for(Country_Code__mdt c : listCountryCode){
           System.debug('Label ->'+ c.Label + ',' + 'Country code ->' + c.Country_Code__c + ',' +'Devloper name ->' + c.DeveloperName);
      }
/* OUTPUT
 * Label -> CANADA,Country code ->CAN,Devloper name ->CANADA
 * Label -> INDIA,Country code ->IND,Devloper name ->INDIA
 * Label -> UNITED STATES,Country code ->UNS,Devloper name ->UNITED STATES
*/
  • getInstance(recordId)
It returns a single custom metadata type sObject record for a specified record ID.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('m022w000000lPl8AAE');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Label ->CANADA,Country code ->CAN
*/
  • getInstance(developerName)
It returns a single custom metadata type sObject record for a specified developerName field of the custom metadata type object.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('CANADA');
System.debug('Developer Name ->'+ CountryCodeRecord.DeveloperName + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Developer Name->CANADA,Country code ->CAN
*/
  • getInstance(qualifiedApiName)
It returns a single custom metadata type sObject record for a qualified API name specified as parameter.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('INDIA');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Label->INDIA,Country code ->IND
*/
 

If you have any questions you can reach out our Salesforce Consulting team here.

No comments:

Post a Comment