Skip to main content

Safe Navigation Operator

 Tired of writing repeated null check statements?? if then here is your new favorite operator(?.) coming from Salesforce as part of Winter 21 release.

We might have come across several instance where we need to write some sequential checks for null references in our code to overcome “NullPointerException” mostly related to data issues with in our org or as required by business.

What the new operator does? If left-hand-side of the chain expression evaluates to null, the right-hand-side is not evaluated. We can use the safe navigation operator (?.) in method, variable, and property chaining. The part of the expression that is not evaluated can include variable references, method references, or array expressions.

Let’s see how we can make use of this new feature in practice.

Assume you have to evaluate an SOQL statement in apex and return response accordingly.

// Previous code checking for nulls
results = [SELECT Name FROM Account WHERE Id = :accId];
if (results.size() == 0) { // Account was deleted
    return null;
}
return results[0].Name;

// New code using the safe navigation operator
return [SELECT Name FROM Account WHERE Id = :accId]?.Name;

When it comes to an object or variable it can be used as follows:

// Previous code checking for nulls
String profileUrl = null;
if (user.getProfileUrl() != null) {
	profileUrl = user.getProfileUrl().toExternalForm();
	}

// New code using the safe navigation operator
String profileUrl = user.getProfileUrl()?.toExternalForm();

Comments

Popular posts from this blog

Identity and access management Flow

Salesforce can participate in a number of standard OAuth 2.0 flows acting as either the client, or as the authorisation and resource server. The flows in this section are the common general flows which are used for either user or server authorisation and authentication - flows to be applied in specific circumstances can be found in the "Specialised OAuth 2.0 Flows" section. The diagrams are shown in general terms as Salesforce can play the role of client or server. Considerations specific to Salesforce are mentioned in the notes. WEB SERVER FLOW Based on the OAuth Authorisation Code Grant flow Gold standard for security if the client secret can be protected by the client application - generally this is the case when the application is operating in a trusted environment (e.g. an internal enterprise server application) Code challenge is optional and used together with code verifier in the token request to provide PKCE (p...

API

API is a application programming interface that allow different application interact with each other.Its deliver the request to your provider that you are requesting it from and deliver the response back. A real example of an API How are APIs used in the real world? Here’s a very common scenario –  booking a flight. When you search for flights online, you have a menu of options to choose from. You choose a departure city and date, a return city and date, cabin class, and other variables like your meal, your seat, or baggage requests. To book your flight, you need to interact with the airline’s website to access the airline’s database to see if any seats are available on those dates, and what the cost might be based on the date, flight time, route popularity, etc. You need access to that information from the airline’s database, whether you’re interacting with it from the website or an online travel service that aggregates information from multiple airlines. Alter...

Salesforce Single Org vs Multi Org Strategy

  Salesforce offers organizations the flexibility to choose between a single org strategy and a multi-org strategy. The choice between these two approaches depends on various factors, including the organization's needs, structure, and long-term goals. Let's explore the benefits, pros, and cons of each strategy and when to choose one over the other. Single Org: What is Single Org?: A single org refers to having all of your Salesforce data, customizations, users, and business processes contained within a single Salesforce instance. Characteristics: All data, such as leads, contacts, opportunities, and custom objects, is stored within one Salesforce instance. Customizations, including custom fields, objects, workflows, and automations, are managed within a single org. User management and permissions are centralized within this instance. Single Org Strategy: Benefits/Pros: Unified Data and Processes: A single org strategy allows you to have all your data and processes within a si...