Salesforce Fact #813 | Null Coalescing Operator in Apex

In Spring'24 release, we have one new operator in Apex i.e. Null coalescing operator. This is an alterative of the null check of any variable. If the left hand side operator is not null return its value else return the value on the right hand side.

Previously:

String name;

String result =  String.isBlank(name) ? 'test' : name;

Now:

String name;

String result = name ?? 'test';

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

Note: It only works with null value, not with other values like 0 or false. Also, this operator is not supported in bind expression in SOQL queries.

Comments

Popular posts from this blog

Salesforce Fact #192 | Call batch apex from flow

Salesforce Fact #457 | Get current user role name in flow