Null Coalescing Operator in Salesforce

In the release of Spring ’24, Salesforce introduced a new feature, the Coalescing Null operator. This operator combines multiple null checks to avoid null pointer exceptions and eliminates the need for multiple if-else conditional blocks.

In this Salesforce tutorial, I will explain the Coalescing Null operator in Salesforce Apex and demonstrate how to coalesce null values in Salesforce Apex and within SOQL queries.

What is a Null Coalescing Operator in Salesforce?

In Salesforce, we use NULL operators to avoid null pointer exceptions. Ideally, we should add multiple if-else blocks in the code if there are multiple Null pointers.

To avoid creating multiple code blocks, we use the Null coalescing operator in the form of a ?? b that returns a if it isn’t null and otherwise returns b.

When we have to check and return null values across multiple fields in SOQL queries, we indirectly use the formula field to coalesce the null field values.

So, the Null Coalescing Operator (??) is used in Apex to return the first non-null value between two operands. We can use conditionals to replicate the null coalescing operator in SOQL.

Use the Null Coalescing Operator in Salesforce Apex

To use the binary null coalescing operator in the Salesforce Apex code, we use it in the form “value1 ?? value2”.

Let’s understand with an example where we can conditionally assign values based on whether a variable is null and how to handle both cases in Apex using the if/else conditionals.

Integer intValue = 10;
Integer outputValue;
if(intValue != null) {
outputValue = intValue;
}
else {
outputValue = 0;
}
System.debug('outputValue is '+outputValue);

Output:

Coalescing operator in Salesforce

Handling Null values for multiple fields:

When we need to handle null values for multiple fields, we must define multiple if-else blocks, as shown in the code below.

Integer outputVal;
Integer a = 10;
Integer b = 20;
Integer c = 60;
Integer d = 85;
if(a != null) {
outputVal = a;
}
else if(b != null) {
outputVal = b;
}
else if(c != null) {
outputVal = c;
}
else if(d != null) {
outputVal = d;
}
else{
Integer outputVal = 100;
}
System.debug('Output value is '+outputVal);

To handle null value pointers in the Integer values of abc, and d. If all the values are null, it will return the default integer value of 100.

We can reduce the lines of code in the above Apex code by using the null coalescing operator (??) to check the null references in the code.

Follow the code below to use the null coalesce operator in Salesforce Apex.

Integer num1 = null;
Integer num2 = 10;
Integer num3 = 30;
Integer num4 = 40;
Integer outputVal = num1 ?? num2 ?? num3 ?? num4 ?? 100;
System.debug('Output value is ' + outputVal);

This will handle the null pointers for all the above-mentioned integer values. In this example, I have entered ‘num1‘ as null, so the desired output is 10.

Output:

Salesforce Coalescing Null field values

We got the output as 10, which is the value of num2. The coalesce operator first iterated on num1 where the value was null, so in the output, the value was of integer num2, which is 10.

When all the integer values are null, then the output will return the default value of 100.

Use Null Coalesce Operator in Salesforce

Output:

Salesforce Apex Null Coalescing Operator

This way, we can handle null pointer exceptions for multiple field values in Apex code using the Null Coalescing Operator (??) in Salesforce Apex.

Use the Null Coalescing Operator in Salesforce SOQL

When using the null coalescing feature in Salesforce SOQL queries, we cannot directly use the Coalescing operator (??).

For example, within the apex code, we can embed the SOQL query and add the coalescing operator outside the query block.

Account defaultAcc = new Account(Name = 'Burlington textiles');
Account acc = [SELECT Id,Name FROM Account WHERE Name = 'Casper Manufacturing
']??defaultAcc;
System.debug('acc value is '+acc.Name);

In the above anonymous code, we have defined the default account name. In the SOQL query [SELECT Id, Name FROM Account WHERE Name = ‘Casper Manufacturing’], if the account name is Null or incorrect, then the output will return the default account name.

In my case, I have an account record, ‘Casper manufacturing,’ so the output will return the account name from the SOQL query.

Coalescing operator in Salesforce SOQL

When the account name is incorrect, the output will return the default account name and handle the null pointer error.

Handle Null values in Salesforce SOQL

Output:

Use Coalesce operator for SOQL query in Salesforce


This way, we can indirectly use the Null Coalesce operator for the Salesforce SOQL queries by following the above steps.

Conclusion

In this Salesforce tutorial, we have learned about the Null Coalescing operator (??) in Salesforce Apex, along with its use cases. By following the above methods, you can use the coalesce operator in your Apex code.

Using the coalesce operator will replace multiple if-else blocks, reducing code complexity and improving readability. It ensures efficient handling of null pointer exceptions and assigns default values when needed.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.