Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Code example showing the usage of the new Apex null coalescing operator in Salesforce development.
Apex

Apex Null Coalescing Operator (Spring '24)

Apex's null coalescing operator (??), added in Spring '24: what it returns, how chaining and short-circuiting behave, and the caveats worth knowing before you use it.

The short answer

The Apex null coalescing operator (??), introduced in Spring '24, returns the left-hand operand if it is not null and falls back to the right-hand operand otherwise. It replaces verbose null-checking boilerplate with concise inline expressions and supports chaining across multiple values.

Key takeaways Use the ?? operator to replace verbose null-check boilerplate when assigning default values. Chain multiple ?? operators together to retrieve the first non-null value from a sequence of variables. Ensure both operands share the same data type or can be promoted to a common compatible type. Write dedicated unit tests for both null and non-null execution paths even when using a single-line coalescing expression.

What Spring '24 added

Spring '24 gave Apex the null coalescing operator, the double question mark (??). It returns the left operand when that operand is not null, and the right operand when it is, which collapses a lot of the null-handling boilerplate you have been writing by hand.

How it works

The syntax:

operand1 ?? operand2

If operand1 is non-null it becomes the result; otherwise operand2 does. If both operands are null, the expression is null.

Common use cases

The two you will hit most are falling back to a default value and getting rid of a verbose null check. A multi-line check collapses into a single expression:

public List getContactsForAccount(Account account) { List contacts = contactsForAccount.get(account.Id);

if (null == contacts)
{
    contacts = new List<Contact>();
}

return contacts;

}

// Becomes

public List getContactsForAccountNew(Account account) { List contacts = contactsForAccount.get(account.Id) ?? new List();

return contacts;

}

Chaining

Chain the operator to take the first non-null value in a sequence:

Opportunity opp = opp1 ?? opp2 ?? opp3 ?? opp4 ?? opp5;

Key considerations

  • Test coverage. A single-line expression lets one unit test exercise both branches for line coverage, which makes it easy to stop there. Test both outcomes anyway, because line coverage is not behavior.
  • Short-circuiting. The right-hand operand is not evaluated when the left-hand operand is non-null, which saves the work and keeps side effects from firing when you did not expect them to.
  • Type compatibility. Both operands must be the same type, or promotable to a common type. Casting to a common type (for example (sObject)) works, but it costs readability.
  • Readability. Do not cram a complex operation or a query into an operand.

Example: inheritance compatibility

public virtual class First { public String name { get; set; } }

public class Second extends First { }

First first = new First(); Second second = new Second(); String name = (first ?? second).name;

Further reading

The release notes, and the language-agnostic background:

Where it pays off

Used with some care, the operator removes boilerplate and makes the intent of a fallback obvious at a glance. Across triggers, controllers and Apex services, where defaults and fallbacks turn up constantly, that means fewer places for a null to slip through and less code to read later. Keep the tests explicit and the operands simple.

Frequently asked questions

What is the null coalescing operator in Apex?

The null coalescing operator (??) is an Apex operator that returns the left-hand operand if it is not null, otherwise returning the right-hand operand.

Does the null coalescing operator short-circuit in Apex?

Yes, the right-hand operand is only evaluated if the left-hand operand is null, preventing unnecessary execution and unintended side effects.

Can you chain the null coalescing operator in Apex?

Yes, multiple coalescing operators can be chained in a single statement to return the first non-null value in the sequence.

What happens if both operands are null in a null coalescing expression?

If both the left-hand and right-hand operands are null, the entire expression evaluates to null.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment