Safe Navigation Operator in Salesforce Apex

In Salesforce Apex development, we must write multiple lines of code to add null checks to identify null objects and prevent null pointer exceptions.

To avoid writing lengthy code for null checks, we can use the Salesforce Safe Navigator Operator, introduced in the Winter ’21 release update.

In this Salesforce tutorial, we will learn what a Safe Navigation Operator is in Salesforce Apex and how to use and implement it in Apex code.

What is a Safe Navigation Operator in Salesforce Apex?

In Salesforce Apex, the Safe Navigation Operator (?.) simplifies null checks when accessing variables or objects that might be null. Using the Safe Navigation operator, we can avoid creating explicit null checks when accessing null objects and variables.

With the help of a safe navigation operator, we can handle and avoid null pointer exceptions in Salesforce. It returns null if any part of the accessed object or variable is null.

For example, if the left-hand side of the chain expression is evaluated to be null, the right-hand side is not evaluated. The code below is an example of how a safe operator handles null values in an expression.

a?.b // Evaluates to: a == null? Null : a.b

In the above expression, first, a ‘ will be evaluated and return null if it is null. Otherwise, the return value will be ‘a.b’.

Use Safe Navigation Operator in Salesforce Apex

In the steps below, we will see the real-time use case of a safe navigation operator in Salesforce Apex. We will also examine the traditional method of handling null values and the approach used by the safe navigation operator.

Let’s consider a scenario where we need to retrieve information about the Account, Contact, and Case objects. In the information, we will fetch the Case Status of a Case linked to a specific Contact associated with an Account.

First, let’s examine the traditional method of adding null checks to fetch the above information by avoiding null pointer exceptions.

Contact contact = [SELECT Id, AccountId FROM Contact WHERE Id = '003J3000008lBSdIAM'];

String caseStatus;
if (contact != null) {
    if (contact.AccountId != null) {
        List<Case> cases = [SELECT Status FROM Case WHERE ContactId = :contact.Id];
        if (!cases.isEmpty()) {
            caseStatus = cases[0].Status; 
        }
    }
}

In the above code, we have used a couple of null checks for the account and the contact object. While the same logic could be written as half of the above code using the Safe Navigation Operator.

Apex Code using Safe Navigation Operator:

Contact contact = [SELECT Id, AccountId FROM Contact WHERE Id = '003J3000008lBSdIAM' LIMIT 1]; //here id is of contact record with an associated account
Id accountId = contact?.AccountId;
String caseStatus = [SELECT Status FROM Case WHERE ContactId = :contact.Id LIMIT 1]?.Status;
System.debug('Case Status: ' + caseStatus);

Now, we will execute the code with the Safe Navigation Operator method.

Output for Null values:

Handle Null Pointer error using safe navigation operator in Salesforce

In the above code, I intentionally entered the Contact ID with no associated case record. If either Contact or Account is null or no related Case is found, then Case Status will be set to null, avoiding the occurrence of a NullPointerException error.

Output for Valid values:

Safe Navigation Operator in Salesforce

When the values were valid (Not Null), we got the output according to the applied conditions.

This is how we can use the Safe Navigation operator in Salesforce Apex, using the method we discussed in the above steps. With this, you can make your Apex code tidier and manageable by avoiding multiple conditional statements for null checks.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.