top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

Fetching Arrays of Data in ServiceNow: A Guide to Using GlideRecord with Client Scripts

Updated: Mar 26


ServiceNow developers often need to retrieve data from server-side tables and utilize it within client-side scripts to enhance user experience and automate processes. One common requirement is to fetch a list of specific field values, such as the unique identifiers (sys_ids) of records, and work with them as an array in a client script. This article addresses a frequent challenge faced when trying to achieve this: correctly passing an array of GlideRecord values from a Script Include to a Client Script.


The Challenge: Decoding "[object Object]" When Expecting an Array

Imagine you're building a ServiceNow solution where you need to identify all users associated with a particular location selected on an Incident form. You might create a Script Include to query the sys_user table based on the location and then attempt to pass the resulting user sys_ids to a Client Script for further processing.

A common initial approach involves iterating through the GlideRecord in the Script Include and pushing the userGR.sys_id directly into an array. However, developers often find that instead of receiving an array of sys_id values in their Client Script, they encounter the enigmatic "[object Object]". This indicates that the entire GlideRecord object, rather than just the desired sys_id, is being added to the array.

Let's examine the typical code structure that leads to this issue:

Client Script (making the AJAX call)

var ga = new GlideAjax('YourScriptIncludeName');
ga.addParam('sysparm_name', 'yourScriptIncludeMethod');
ga.addParam('sysparm_location', g_form.getValue('u_location')); 
// Assuming 'u_location' is the location field on the form
ga.getXML(AjaxParse);

function AjaxParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  var userSysIds = JSON.parse(answer);
  for (var i = 0; i < userSysIds.length; i++) {
    // Process each userSysId
    alert(userSysIds[i]);
  }
}

Script Include (attempting to return the array)

var YourScriptIncludeName = Class.create();
YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  yourScriptIncludeMethod: function() {
    var locationSysId = this.getParameter('sysparm_location');
    var users = [];
    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('location', locationSysId);
    userGR.query();
    while (userGR.next()) {
      users.push(userGR.sys_id); // This is where the issue lies
    }
    return JSON.stringify(users);
  },
  type: 'YourScriptIncludeName'
});

The problem in the Script Include lies in this line: users.push(userGR.sys_id);. Here, userGR.sys_id refers to the sys_id property of the GlideRecord object itself, which evaluates to "[object Object]" when stringified directly. To get the actual sys_id value (a string), you need to explicitly retrieve it.


The Solution: Extracting the sys_id Value Correctly

To correctly push the sys_id values into the array, you need to use either the getValue('sys_id') method or the getUniqueValue() method of the GlideRecord object. Both methods will return the sys_id as a string.

Here's the corrected Script Include code

var YourScriptIncludeName = Class.create();
YourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  yourScriptIncludeMethod: function() {
    var locationSysId = this.getParameter('sysparm_location');
    var users =;
    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('location', locationSysId);
    userGR.query();
    while (userGR.next()) {
	  // Correct way to get the sys_id
      users.push(userGR.getValue('sys_id')); 
      // OR
      // users.push(userGR.getUniqueValue()); // Another correct way
    }
    return JSON.stringify(users);
  },
  type: 'YourScriptIncludeName'
});

By using userGR.getValue('sys_id') or userGR.getUniqueValue(), you are explicitly asking for the string representation of the sys_id, which is what you intend to store in your array. When this array is then stringified using JSON.stringify() and passed back to the Client Script, it will be correctly parsed into an array of sys_id strings.


Understanding GlideAjax Communication

This process relies on ServiceNow's GlideAjax functionality. Here's a breakdown of how it works in this context:

  1. Client Script Initiates the Request: The Client Script creates a GlideAjax object, specifying the name of the Script Include to call.

  2. Parameters are Passed: Using ga.addParam(), the Client Script sends the name of the method to execute in the Script Include (sysparm_name) and any other necessary parameters (in this case, the location sys_id using a custom parameter sysparm_location).

  3. Script Include Processes the Request: The specified method in the Script Include (yourScriptIncludeMethod) receives the parameters, performs the GlideRecord query, and constructs the array of user sys_ids using the correct method to extract the values.

  4. Response is Sent Back: The Script Include returns the array as a JSON string using JSON.stringify().

  5. Client Script Handles the Response: The AjaxParse function in the Client Script receives the response. The response.responseXML.documentElement.getAttribute("answer") line retrieves the stringified array.

  6. JSON is Parsed: JSON.parse(answer) converts the JSON string back into a JavaScript array, which can then be iterated over and used in the Client Script.


Practical Use Cases

This technique of fetching arrays of data is valuable in various ServiceNow scenarios, such as

  • Populating a list of users based on a selected department or location.

  • Retrieving a list of active incidents associated with a particular user.

  • Dynamically filtering options in a choice list based on server-side data.

  • Performing bulk updates or validations based on a set of records fetched from a table.


Conclusion: Mastering Data Retrieval in ServiceNow

Successfully retrieving and utilizing data between client-side and server-side scripts is a fundamental skill for ServiceNow developers. When working with GlideRecord objects and arrays, it's crucial to remember that you need to explicitly extract the desired field values using methods like getValue() or getUniqueValue() rather than directly pushing the entire object. By understanding this key concept and leveraging the power of GlideAjax, you can effectively build dynamic and data-driven ServiceNow applications. Always remember to test your scripts thoroughly to ensure they function as expected and handle various scenarios gracefully.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

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