How to fix it
- Add debug logging before calling
getRefRecord()to check if your reference field has a value:gs.log('Reference field value: ' + gr.fieldName.toString()); - If the field is empty, check your Business Rule conditions and when it executes. Navigate to
System Definition > Business Rulesand verify the rule runs after the reference field is populated. - 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');
}
- 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 usinggr.get(sys_id). - 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'. - 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.