Safe Navigation Operator in Apex
Safe Navigation Operator in Apex
Safe Navigation Operator is released as a part of Winter 21 update.
The main advantange of Safe Navigation Operator is avoid cluttered code when there are too many null checks and make it look lot Β more verbose.
Safe Navigation Operator is not something new that's introduced in Apex, most of the Object Oriented Programming languages has this concept.
Let's look at an example where we might come across a Null Pointer Exception and I will show you how to use Safe Navigation Operator to make the code lot more verbose.
Here, in Step 1 am trying to fetch 5 random account records, think of that as list of records that are accumulated as a result of some business logic.
Then, in step 2 am fetching all the account records and storing them in a Map.
In Step 3, am fetching account record using the key which is going to be the account record Id.
If there exists no record and if we receive null in Step 3, In step 4 when I use the nottaion localAccount.Site.substring(8)
it is going to give us Null Pointer Exception.
Likewise, assume we get back a record in Step 3 but there exists no value for the field site
in the received account record, in that case we might again encounter Null Pointer Exception because we are using substring()
method on null value.
To encounter that, we need to refactor the code a bit and it's going to look like this
But, don't you think Step 3 and 4 are looking a bit clutterd?
Here comes Safe Navigation Operator to our rescue. Look at the refactored code below.
Have an extra pair of eyes on this filteredAccountSites.add(accountsMap.get(a.Id)?.Site?.Substring(8));
This looks lot more easy on this eyes, isn't it?
Basically, the notation to the left of the ?
, if it's not null then the actual data will be returned, on top of which we can invoke other functions.
In case the notation to the left of ?
is null, then it returns null. Thus avoiding the Null Pointer Exception.