How to fix it

  1. Add debug logging before calling getRefRecord() to check if your reference field has a value: gs.log('Reference field value: ' + gr.fieldName.toString());
  2. If the field is empty, check your Business Rule conditions and when it executes. Navigate to System Definition > Business Rules and verify the rule runs after the reference field is populated.
  3. Always validate the returned GlideRecord before using it. Replace direct property access with this pattern:

var refRecord = gr.fieldName.getRefRecord();
if (refRecord.isValidRecord()) {
// Safe to use refRecord properties
gs.log('Referenced record found: ' + refRecord.getValue('name'));
} else {
gs.log('No valid referenced record found');
}

  1. If working with a new record, ensure you're setting the reference field value before calling getRefRecord() or query the record from the database first using gr.get(sys_id).
  2. Verify the field is actually a reference field by navigating to System Definition > Tables & Columns, filtering by your table name, and confirming the Type is 'Reference'.
  3. If the referenced record might be deleted, add a fallback check by querying the target table directly:

var targetRecord = new GlideRecord('target_table');
if (targetRecord.get(gr.getValue('reference_field'))) {
// Record exists, safe to use
} else {
gs.log('Referenced record no longer exists');
}

💡

Test your fix by adding gs.log statements before and after getRefRecord() calls, then check System Logs > System Log > All to see the actual field values and validation results.