Showing posts with label Avoid Soql using Custom Metadata Type. Show all posts
Showing posts with label Avoid Soql using Custom Metadata Type. Show all posts

Monday 20 December 2021

Avoid Soql using Custom Metadata Type - Platform Developer I Certification Maintenance (Winter '22)

Code without getInstance - 

public class CountryCodeHelper {
  public static string getCountryCode(String country) {
    Country_Code__mdt countryCode = [
      SELECT Id, MasterLabel, Country_Code__c
      FROM Country_Code__mdt
      WHERE MasterLabel = :country
      LIMIT 1
    ];
 
    return countryCode.Country_Code__c;
  }
}

Efficient code by avoiding soql and leveraging getInstance method for custommetadata Types - 

Use the getInstance Method to Retrieve Custom Metadata

public class CountryCodeHelper {
  public static string getCountryCode(String country) {
   Country_Code__mdt countryCode= Country_Code__mdt.getInstance(country);
    return countryCode.Country_Code__c;
  }

Platform Developer I Certification Maintenance Winter 22 Trailhead/Certification Maintenance link here