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.bIn 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:

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:

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:
- Ternary Operator in Salesforce Apex
- Operators in Salesforce Apex
- Polymorphism in Salesforce Apex
- Null Coalescing Operator in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.