Posts

Showing posts with the label Safe Navigation Operator

Use of Safe Navigation Operator

Safe Navigation Operator Instead of using explicit, sequential checks for null references, use the safe navigation operator (?.). This new operator short-circuits expressions that attempt to operate on a null value and returns null instead of throwing a NullPointerException. NOTE: All Apex types are implicitly nullable and can hold a null value returned from the operator. Example - Account acc = [SELECT Name FROM Account WHERE Id = :accId]; if (acc != null) {     return acc.Name; } Using Safe Navigation Operator String accName = acc?.acc.Name; String accName =  [SELECT Name FROM Account WHERE Id = :accId]?.Name; The Safe Navigation Operator (?.) in Apex is a feature that allows developers to safely navigate objects and data structures, avoiding null pointer exceptions. When you use the Safe Navigation Operator to access a chain of references, Apex will evaluate the expression from left to right and return null immediately if it encounters a null reference rather tha...