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

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

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

Salesforce Platform Cache

  Salesforce Platform Cache is a feature that allows you to store and manage data in-memory, improving the performance of your Salesforce applications. It provides two types of caches: Org Cache and Session Cache. Org Cache is shared across all users in your Salesforce organization, while Session Cache is specific to a single user's session. Here, I'll provide an example of how to use Salesforce Platform Cache with Apex. Example Scenario: Let's consider a scenario where you have a Salesforce custom object called "Product__c," and you want to cache the product data for better performance. Step 1: Enable Platform Cache Before you can use Platform Cache, you need to enable it in your Salesforce org. To do this, go to Setup > Quick Find Box > Type "Platform Cache" > Select "Platform Cache." Step 2: Create a Cache Partition Create a cache partition, which is like a container for storing data in Platform Cache. Each partition has a unique...