Header Ads Widget

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 #1


With 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 snippet

public 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 snippet

public static void fetchCapital(String countryName) {
    Country__mdt singleCountry = Country__mdt.getInstance(countryName);
    System.Debug('Country Name: ' + countryName + ' Capital: '
        + singleCountry?.Capital__c);
}



Feature #2


With 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: ' + test18.to15());
}



Feature #3



Starting Spring 21, we can pass strings with single quotes in the format() method from System.String class. 

If we execute the below code snippet in any org which is not Sping 21 release, we will find the output as -

String template = '{0} \'s view was last updated {1}';
List<Object> parameters = new List<Object> {'Universal Containers', DateTime.newInstance(2018, 11, 15) };
String formatted = String.format(template, parameters);
System.debug ('Newly formatted string is:' + formatted);

Output:
Newly formatted string is:Universal Containers s view was last updated {1}

As you can see because of single quotes in the stringToFormat parameter, our expected result was wrong. It was not able to replace {1}.

Now if we execute the same code in a Spring 21 org, the output we will get is -

Newly formatted string is:Universal Containers's view was last updated 2018-11-15 05:00:00

That's awesome. So starting Spring 21, we will be able to pass single quoted in the stringToFormat parameter for the format() method.




Feature #4


With Spring 21, you can flag Invocable Apex Methods that make callouts. The syntax is 

@InvocableMethod(callout=true, label="Hello World")

By making the callout attribute true, you are basically telling Salesforce to take the decision (best decision) whether to run the callout in the currently running transactions or in a new transaction.

With the callout attribute set to false, action will always happen in the current transaction whether the transaction has uncommitted work, which can cause some expected issues.


Feature #5



If the SOQL queries are having a for loop, then Apex will no longer generate or use the query cursor internally. Instead, records are buffered in memory for improved performance and reduced query cursor contention. Though this improvement is not applicable to Batch Apex queries.

Feature #6



Starting Spring 21, you can now convert leads into a Person and Business Account at the same time with the new LeadConvert methods. Note - This feature is only available in Lightning and in Enterprise, Performance, and Unlimited editions.

Here are the methods available -

You can find code examples here.



Feature #7



The Transaction Finalizers feature is in Beta as of Spring 21. Basically, Transaction Finalizer will make sure that any failed (due to some unhandled exceptions) Queueable job can be re-queued only for 5 times (that's the new limit). The counter will reset when the Queueable job completes without an unhandled exception. 

Here are the methods -




You can find an example code snippet here.

Please provide your feedback. Thank you.

Post a Comment

0 Comments