Reference fields in ServiceNow behave differently than regular fields when they're empty. While you can safely check current.state == 6 on a choice field, trying to access properties on an empty reference like current.assigned_to.email will throw a null pointer exception that crashes your script. The platform doesn't automatically guard against this because reference field objects are lazy-loaded β ServiceNow doesn't know the reference is empty until you try to access it. Always null-check references before accessing their properties or methods.
When to use this
- Before accessing any property or method on a reference field (
current.assigned_to.name,current.caller_id.getDisplayValue()) - In Business Rules where reference fields might be empty during record creation or updates
- Before using a reference field value in another GlideRecord query or API call
- When processing records in scheduled jobs where data integrity isn't guaranteed
When NOT to use this
- Don't use
nil()in client scripts β it's server-side only. Useg_form.getValue('reference_field')instead - Don't check references you've just set in the same script β
current.assigned_to = userId; current.assigned_to.nil()will still return true - Don't use this pattern for choice fields or string fields β only reference fields throw null pointer exceptions
Key behaviors and gotchas
- Use
!gr.reference_field.nil()orgr.getValue('reference_field')β both return falsy when the reference is empty - Testing
if (current.assigned_to)will throw an exception if the field is empty β the field object itself is null - Empty reference fields store empty strings in the database, but
nil()checks for both empty strings and actual nulls - ACL restrictions can make reference fields appear empty even when they contain valid sys_ids β always check access rights in complex scenarios
- Cross-scope reference fields may return empty even with valid data if the target table is in a different application scope
- Domain separation affects reference field visibility β records in different domains may appear as empty references
Never chain reference field access without null checking each level. current.assigned_to.manager.name will crash if either assigned_to or manager is empty. Check each reference in the chain separately.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.