How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)

By | August 14, 2021

In this blog, we will discuss how to Avoid Null Pointer Exception that occurred on various conditions by using the Safe Navigator Operator. In the recent update of Salesforce, Winter’21 introduced an Operator Named As Safe Navigator Operator(?.).

Certainly, it is efficient & easy to understand the syntax for Salesforce Developers, making the coding clean. In short, using this operator no need to write code for handles explicit, sequential checks for null references. In conclusion, this operator has short-circuited expressions.

Syntax: – e.g.: – a?.b // Evaluates to: a == null ? null : a.b

If the left-hand side of the chain expression figures out to null, the right-hand side isn’t classified. In conclusion, using the safe navigation operator (?.) in method, variable, and property chaining. To clarify, the part of the expression that isn’t classified can include variable references, method references, or array expressions.

Explaining Operator use by elaborating piece of Code is as follow: –

Firstly, the below example shows a single statement replacing a block of code that checks for nulls.

List<String> ListAccountSites = new List<String>();

//Step: 1
List<Account> ListAccounts = [SELECT Id, Name, Phone FROM Account LIMIT 10];

//Step: 2
Map<Id, Account> MapAccount Details= new Map<Id, Account>([SELECT Id, Name, Phone FROM Account LIMIT 10]);

for(Account a : ListAccounts){
    //Step: 3	
    Account NewAccount = MapAccount.get(a.Id);
    if(NewAccount!= null){
    	if(NewAccount.site != null){
        	ListAccountSites.add(NewAccount.Phone);
        }
    }
}

Finally, the above Example by using Safe Navigator Operator (?.):-

List<String> ListAccountSites = new List<String>();

//Step: 1
List<Account> ListAccounts = [SELECT Id, Name, Phone FROM Account LIMIT 10];

//Step: 2
Map<Id, Account> MapAccount Details= new Map<Id, Account>([SELECT Id, Name, Phone FROM Account LIMIT 10]);

for(Account a : ListAccounts){
        	ListAccountSites.add(MapAccount.get(a.Id)?.Phone);
} 

Above all, we can use this Operator with a top-level statement, SObject chaining, SOQL Queries, etc.; for more details, refer to the below references.

We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please get in touch with us at salesforce@greytrix.com.

References:-

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm

About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts