Salesforce Development Tutorial: How to use the Safe Navigation Operator in Apex to reduce the amount of null checking in your codebase

Why use the Safe Navigation Operator??

My guy, this is the best thing Salesforce has added to the Apex language in forever! Embrace it! To elaborate on that though, let me hit you with this thrilling sales pitch. Just imagine a late night commercial selling you garbage, but the garbage is actually good.

Are you frustrated with the insane amount of null checking you have to do when writing your apex code? Do you wish there was a way you didn’t have to do a null to prevent your code from crashing and burning? Well Salesforce has a solution to that problem! The safe navigation operator! Starting with the Winter 21 release your dreams have finally come true! Erase all those null checks you were doing on your soql queries, your objects and so much more! Watch 4+ lines of code get condensed to a single line and enjoy your life as a Salesforce Developer just a little bit more!

Ok… enough of that, hopefully that sweet action sales pitch sold you, but if it didn’t let’s check out some code to hopefully convince you the safe nav operator is worth your time.

Safe Navigation Operator Code Examples

Before we get started, if you’d prefer to check out these examples on GitHub, you can see them here. Otherwise, let’s keep on keepin on. Let’s start with a soql example that doesn’t use the safe navigation operator. In this example we just want to get the name of an Account.

public static void noSafeNav(){
        List<Account> acctList = [SELECT Id, Name FROM Account WHERE Name = 'Kevin' LIMIT 1];
            String acctName = acctList[0].Name;

As you can see above, to navigate safely without the risk of our code crashing from a null pointer exception or a, “list has no rows to assign to SObject” exception we need 4 lines of code. Not bad, but after awhile that can get tedious. Now let’s try to do the same thing with the safe navigation operator.

public static void safeNav(){
        String acctName = [SELECT Id, Name FROM Account WHERE Name = 'Kevin' LIMIT 1]?.Name;

Oh dwamnnnnnnnnnnnnn, with the safe navigation operator we are able to do this in a single line of code… uhmazing. If you didn’t know the safe navigation operator is that question mark between the soql query and the .Name. Basically what the safe navigation operator does is check whether the query is null before trying to grab the name field from the SOQL query. If the query is null (or returns nothing), it will assign the value null to the acctName variable. This is super convenient and can reduce your codebase a ton.

Hopefully this example has convinced you it’s worth it to utilize the safe navigation operator but if it hasn’t there is a mapping example in the github repo too. It can also save you a ton of null checking with maps as well!

Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!

Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture

Similar Posts