What It Does

The getRefRecord() method returns a GlideRecord object representing the record referenced by a reference field. When ServiceNow queries a record containing reference fields, it automatically populates those reference field objects with basic information about the referenced records (sys_id, display value, table name). This method provides access to that pre-loaded referenced record without requiring an additional database query.

Internally, ServiceNow maintains a cache of referenced record data when it loads the parent record. The getRefRecord() method accesses this cached data and wraps it in a GlideRecord object. This cached GlideRecord contains all fields that were included in the original query's reference field expansion, plus any fields explicitly added through addQuery() or addJoinQuery() operations.

The method returns null in several scenarios: when the reference field is empty, when it points to a record that no longer exists, or when the current user lacks read access to the referenced record. The returned GlideRecord, when not null, behaves exactly like any other GlideRecord object—you can call getValue(), access field properties, and even call getRefRecord() on its reference fields for nested reference traversal.

A critical edge case occurs when you need field data that wasn't included in the original query. The returned GlideRecord only contains fields that ServiceNow loaded during the initial query. If you access a field that wasn't pre-loaded, ServiceNow will execute a database query to fetch that specific field's value. This behavior differs from calling get() with the reference field's sys_id, which would reload the entire referenced record.

The method is closely related to getDisplayValue() and toString() on reference fields. While those methods return the display value of the referenced record, getRefRecord() provides full access to all available fields on the referenced record. Use getTableName() on the reference field when you only need to identify the referenced record's table.

When to Use This

Use getRefRecord() when you need to access multiple fields from a referenced record or when you need to perform operations on the referenced record object itself. This method is particularly valuable in Business Rules where you're processing records and need efficient access to reference field data without the performance cost of additional database queries. It's also essential when building complex conditional logic that depends on multiple attributes of the referenced record.

Avoid this method when you only need the display value of the reference field—use getDisplayValue() instead. When you need to modify the referenced record, use a fresh GlideRecord with get() rather than the cached record returned by getRefRecord(). The cached record may not contain all fields needed for updates and could lead to unexpected behavior when calling update().

⚠️

Never call update() or delete() on a GlideRecord returned by getRefRecord(). The cached record may be incomplete and could cause data integrity issues.

Return Value

Returns a GlideRecord object representing the referenced record, or null if the reference is empty or invalid. The returned GlideRecord contains field data that was loaded during the original query, including the sys_id, display value, and any fields explicitly included through joins or reference field expansion. This GlideRecord is positioned (no need to call next()) and ready for immediate field access.

Always check for null before using the returned GlideRecord. Reference fields can be empty, point to deleted records, or reference records the current user cannot access due to ACL restrictions. Use if (refRecord && refRecord.isValidRecord()) for the most robust null checking pattern.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No Business Rules, ACLs, or other platform mechanisms fire when accessing the cached referenced record
  • No database queries execute unless you access fields that weren't included in the original query
  • Performance is excellent—accessing cached reference data is nearly instantaneous
  • Field access security is enforced based on the current user's ACL permissions to the referenced table
  • Works identically in before and after Business Rules, Script Includes, and Scheduled Jobs
  • Reference field data may be stale if the referenced record was modified after the parent record was loaded
  • Calling setValue() or update() on the returned GlideRecord may not behave as expected due to incomplete field data