Using Groovy's Safe Navigation Operator

If you are using "dot" notation to navigate to reference the value of a related object, you should use Groovy's safe-navigation operator ?. instead of just using the . operator.

This will avoid a NullPointerException at runtime if the left-hand-side of the operator happens to evaluate to null. For example, consider a TroubleTicket object with a lookup field named AssignedTo representing the staff member assigned to work on the trouble ticket. Since the AssignedTo field may be null before the ticket gets assigned, any code referencing fields from the related object should use the safe-navigation operator as shown here:

// access related lookup object and access its record name
// Using the ?. operator, if related object is null,
// the expression evaluates to null instead of throwing
// NullPointerException
def assignedToName = AssignedTo_Obj_c?.RecordName
Tip: For more information on why the code here accesses AssignedTo_Obj_c instead of a field named AssignedTo_c, see Understanding Secondary Fields Related to a Dynamic Choice List Field