This post will cover some of my favorite Apex new features from Winter ’21. I have published another post that covered new existing enhancements for Flow Designer. To explore all the new features from Winter ’21 release, I highly recommend –
  • Sign up to a pre-release org and explore all the new features.
  • Post your findings in the Trailblazer Community and include the hashtag #Winter21Treasure to receive Trailhead Treasure Hunter badge
Here is the list

Safe Navigation Operator

With the introduction of Safe Navigation operator (?.), we are no more needed to perform an explicit null check to avoid NullPointer Exception. Basically, if the left side expression of the operator evaluates to null, then the right side will not be evaluated. 
//Before Winter’21
List<Contact> allContacts = [
FROM Contact
WHERE name = :contactName
if (allContacts.size() > 0) {
String Id = allContacts.get(0).Id;

//After Winter’21
String Id = [SELECT Id FROM Contact WHERE name = :contactName]?.get(0).Id;

Update Resources with the PATCH HTTP Method in Apex Callouts

Before Winter ’21 it was not possible to make a PATCH request from Salesforce. We always used the hack of using the PUT method to make it work. But with Winter ’21 we can now make PATCH metho in our HttpRequest class.

Detect Apex Runtime Context with RequestId and Quiddity

Now it is possible to detect Apex context at runtime and correlate multiple logs triggered by the request, using Request ID and Quiddity values.
//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();

Improve Apex Testing with New SObject Error Methods

With the SObject.hasErrors() and SObject.getErrors() methods we can check the result for errors without performing aany DML operation. Basically, SObject.addError() will add the error on SObject instance, then SObject.hasErrors() will return true and SObject.getErrors() method returns a list of Database.Error objects that contain the errors encounter.
//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());

Send Custom Notification from Apex

You can use the Messaging.CustomNotification class to create, configure, and send custom notifications directly from your apex code. You need to create a new instance of CustomNotification, configure a few notification details, and finally call the send() method.

Enhanced Support for @namespaceAccessible Annotation

The @namespaceAccessible annotation now supports interfaces, properties, and abstract classes. To know more, read it here
Please let me know your favorite one in the comment.