Apex Winter Release 21

Salesforce Apex Winter Release 21 - All You Need To Know

1. Before Apex Winter Release 21 batch limit for big objects using deleteImmediate() is 2,000 records at a time.

Now we can delete up to 50,000 big object records at once using Database.deleteImmediate() method. 

global class DeleteBatchExample implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, Name ,Email FROM Account';
        return Database.getQueryLocator(query);
    }     
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        Database.deleteImmediate(accList);        
   }        
   global void finish(Database.BatchableContext BC) {   
   }
}

2. Salesforce Introduced Safe Navigation Operator to Avoid Null Pointer Exceptions

In Apex Winter Release 21, To avoid Null Pointer Exceptions Salesforce introduced a new operator called Safe Navigation Operator.

Null Pointer Exceptions: It is a very common error [Attempt to de-reference] which occurs when we try to use a null object or when a particular field value is null that line of code is searching.

// Before Apex Winter 21 Release code to avoid null pointer exception
String Urlprofile = null;
if (user.getProfileUrl() != null) {
    Urlprofile   = user.getProfileUrl().toExternalForm();
}
// To Avoid null Pointer Exception use Safe Navigation Operator
String profileUrl = user.getProfileUrl()?.toExternalForm();

dont miss out iconDon't forget to check out: Apex Integration in Salesforce - The Developer Guide

3. After Apex Winter 21 Release, We can Update Resources with PATCH  Method in Apex Callouts

4. Improve Apex Testing with New SObject Error Methods

In Apex winter 21 Release, Salesforce Introduced two new methods hasErrors() and getErrors() to track errors without performing a DML operation to check the result for errors.

//Baseline code sample for using addError, getErrors, together
   Account a = new Account();
   String msg = 'New error method in SObject';
   //The new overload that accepts the field dynamically at runtime
   a.addError('Name', msg); 
   List<Database.Error> errors = a.getErrors();
   System.assertEquals(1, errors.size());
   Database.Error error = errors.get(0);
   System.assertEquals(msg, error.getMessage());
   System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, error.getStatusCode());
   String[] fields = error.getFields();
   System.assertNotEquals(null, fields);
   System.assertEquals(1, fields.size());

5. Send Custom Notifications from Apex

Now we can send custom notifications from Apex code without making API callouts because winter 21 Release Salesforce provides the Messaging.CustomNotification class to create, configure, and send custom notifications.

6. Detect Apex Runtime Context with RequestId and Quiddity

We can now detect Apex context at runtime and correlate multiple logs triggered by the request, using Request ID and Quiddity values.

 Use the methods in the System.Request class to obtain the Request ID and Quiddity of the current Salesforce request.

We can  retrieve Quiddity and its short-code, use the Request.getQuiddity() and System.getQuiddityShortCode() methods.  

dont miss out iconCheck out another amazing blog by Kirandeep here: Publishing an App on Salesforce AppExchange

This example shows how to retrieve and use Request ID and Quiddity.

//Get info about the current request
Request reqInfo = Request.getCurrent();
//Universally unique identifier for this request
//Same as requestId in splunk or REQUEST_ID in event monitoring
String currentRequestId = reqInfo.getRequestId();
//enum representing how Apex is running. e.g. BULK_API vs LIGHTNING
//Use this with a switch statement,
//instead of checking System.isFuture() || System.isQueueable() || ...
Quiddity currentType = reqInfo.getQuiddity();

 

Responses

Popular Salesforce Blogs