Top Apex Features From Spring 21
Spring 21 is bringing a couple of Apex features that can be definitely helpful. This post will cover a few of my favorite Apex features from Spring 21.You can find all my Spring 21 release posts here.Feature #1With Spring 21, we can fetch the entire custom metadata records or a single record from the custom metadata. So no more SOQL query to fetch custom metadata records. Here is the video where I have explained the feature. With getAll() method, now we can get the entire custom metadata records. Here is the small code snippetpublic static void fetchAllMetadata() { Map<String, Country__mdt> allCountries = Country__mdt.getAll(); System.Debug(‘allCountries Size: ‘ + allCountries.size()); for(String countrName : allCountries.keySet()){ System.Debug(‘Country Name: ‘ + countryName + ‘ Capital: ‘ + allCountries.get(countrName).Capital__c); }}With getInstance( recordId ), getInstance( qualifiedApiName ), getInstance( developerName ) method, now we can get the single custom metadata record. Here is the small code snippetpublic static void fetchCapital(String countryName) { Country__mdt singleCountry = Country__mdt.getInstance(countryName); System.Debug(‘Country Name: ‘ + countryName + ‘ Capital: ‘ + singleCountry?.Capital__c);}Feature #2With Spring 21, we can now convert 18-character Id to 15-character Id. Here is the video where I have explained the feature. With to15() method, now we can get the 15-character Id of the corresponding 18-character Id. Here is the small code snippet.public static void convertId(String id_18) { ID test18 = id_18; System.Debug(’15 Digit ID: ‘...
Read More