top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

Understanding the getReference() Method in ServiceNow: Usage and Best Practices

Updated: Mar 28

In ServiceNow, efficient client-side scripting significantly enhances user experience by automating data retrieval and field population. One essential method frequently used in client scripts is getReference(). While powerful, improper usage can lead to performance issues. In this article, we'll explore the purpose of getReference(), illustrate its correct usage, discuss common pitfalls, and provide recommended alternatives.


What is the getReference() Method?


The getReference() method is utilized within ServiceNow client-side scripts to retrieve the complete record from a reference field. By accessing the entire record, scripts can dynamically populate fields based on associated reference information, thereby streamlining user interactions and reducing manual data entry.


Proper Usage and Practical Example


Here's a practical scenario illustrating correct usage:


Suppose you have a service catalog form where selecting a user in a "Requested For" field should automatically populate another field, such as "Location," based on the user's record.


Correct Implementation Using Callback Function (Recommended):

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   g_form.getReference('requested_for', populateLocation);

   function populateLocation(userRecord){
      g_form.setValue('location', userRecord.location.toString());
   }
}

In this example:

  • getReference('requested_for', callbackFunction) retrieves the selected user's record asynchronously.

  • The callback function (populateLocation) then sets the location field to match the retrieved user's location.


Synchronous vs. Asynchronous Usage

Synchronous Method (Not Recommended)


var userRecord = g_form.getReference('requested_for');
if(userRecord.vip == 'true') {
   alert('User is a VIP!');
}

Drawbacks:

  • Temporarily freezes the form until the record is retrieved.

  • Poor user experience, especially noticeable on slow network connections.


Asynchronous Method (Recommended)


function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   g_form.getReference('requested_for', function(userRecord) {
      if(userRecord.vip == 'true'){
         alert('User is a VIP!');
      }
   });
}

Benefits:

  • Non-blocking, allowing users continued interaction.

  • Enhances overall system responsiveness.


Common Pitfalls and Alternative Solutions


While useful, relying heavily on getReference() can significantly impact performance due to frequent server calls. Here are some considerations and alternatives:


  • Performance Impact: Excessive synchronous calls can degrade form performance. Always prefer asynchronous calls.


  • Alternative Solution: Use GlideAjax with asynchronous callbacks for better performance when fetching data frequently or from large tables.


Example Using GlideAjax (Recommended Alternative)


function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var ga = new GlideAjax('GetUserLocation'); // Server-side script include
   ga.addParam('sysparm_name', 'fetchLocation');
   ga.addParam('sysparm_user', newValue);
   ga.getXMLAnswer(function(response) {
      g_form.setValue('location', response);
   });
}

This approach minimizes client-side performance issues by delegating the data retrieval logic to server-side scripts.


Conclusion


The getReference() method in ServiceNow is valuable for accessing detailed reference field data directly within client scripts. To ensure optimal performance, it is crucial to use asynchronous methods responsibly or consider alternatives like GlideAjax for data-intensive operations. Following these best practices enhances your application's responsiveness and improves user satisfaction.


Next Steps


  • Review existing scripts to replace synchronous getReference() calls with asynchronous implementations.

  • Explore GlideAjax and server-side scripting solutions for high-performance client-side operations.

  • Regularly monitor and optimize form performance based on user feedback and usage data.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page