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