Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
Best practice when using getRefRecord() - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Best practice when using getRefRecord()
KB0745222

Best practice when using getRefRecord()


50653 Views Last updated : Sep 7, 2023 public Copy Permalink English (Original)
  • English (Original)
  • Japanese
KB Summary by Now Assist

Issue

getRefRecord() returns a GlideRecord object for a given reference element. This is used widely in business rule scripts but incorrect usage of this can cause some major issues. This article describes some issues seen and the best practice to follow to avoid these. 

Issues

  1. Printing values from the record obtained using getRefRecord() might not print anything. 
  2. Orphan records get inserted when updating the record obtained using getRefRecord(). 
  3. Using values from record obtained using getRefRecord() in other script results in "undefined" errors.


Resolution

If the reference field has an empty value, getRefRecord() doesn't throw any error but it does return a GlideRecord object. That's why it is important to check if the returned object is a valid GlideRecord object or not. 

1
2
3
4
5
6
7
var grUser = current.user.getRefRecord();
if(grUser.isValidRecord()) { // << only perform operations on it if it's a valid record
    if(grUser.email != current.email_address){
        grUser.email = current.email_address;
        grUser.update();
    }
}

getRefRecord() should always be followed by isValidRecord() check. 

If expected field is on a child table, make sure the reference field points to the child table and not the parent table.  Otherwise the getRefRecord() returns the GlideRecord object from the parent table which will not have the field that exists on the child table resulting in "undefined" errors.


The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.