ServiceNow: Passing Values to Arrays during GlideRecord Queries

Ryan A
4 min readFeb 8, 2024

Some very intelligent people on Stack Overflow will tell you that JavaScript does not actually have any sort of pass by reference. In fact, JavaScript only has “pass by value” and “pass by sharing”, in which what is passed is not the original reference but a copy of the reference.

None of this matters for what we’re about to talk about, but you can find a very good explanation of this here.

We are still going to call this “pass by reference” for now, because “reference” is already a well-understood term in ServiceNow.

We have a good idea of what a reference field is in a table.

If we stored the Caller name directly on an Incident, in a String field… what would happen if that caller changed their name? Suddenly we’d have some Incidents tied to an old name and some tied to a new name. So instead, we reference another record. The caller name is being pulled from the User table instead. What we are storing in Caller is not actually the user’s name, but a reference to the record on the User table.

INC0009009 — David Miller’s name is not stored on the Incident, but on the User table.

This means, if David Miller changed his name, that would be changed in the User table, and all the Incidents would now display the new name instead — both all his previous Incidents and any new Incidents he opened. Pretty neat.

We can think of arrays and objects in JavaScript the same way. When we pass data between objects and arrays, what is passed is a reference to where we can find the data.

Let’s look at GlideRecord as an example!

var gr = new GlideRecord('incident');
gr.addActiveQuery;
gr.setLimit(5);
gr.query();
while (gr.next()) {
gs.print(gr.short_description);
}

This will give us:

*** Script: Unable to connect to email
*** Script: My computer is not detecting the headphone device
*** Script: Reset my password
*** Script: Need Oracle 10GR2 installed
*** Script: Need new Blackberry set up

Makes sense. We are printing every time we are looking at a new record in our GlideRecord query.

What if, instead, we put these in an array?

var array = [];

var gr = new GlideRecord('incident');
gr.addActiveQuery;
gr.setLimit(5);
gr.query();
while (gr.next()) {
array.push(gr.short_description);
}

for (item in array) {
gs.print(array[item]);
}
*** Script: Need new Blackberry set up
*** Script: Need new Blackberry set up
*** Script: Need new Blackberry set up
*** Script: Need new Blackberry set up
*** Script: Need new Blackberry set up

Uh oh! What’s happened here?

Well, every time we pushed to array, we thought we were pushing the string of the short description… but instead, we were pushing that reference to gr!

So, what was actually stored in the array looked something like this:

[
gr.short_description,
gr.short_description,
gr.short_description,
gr.short_description,
gr.short_description
]

When we printed out the end result, gr.short_description’s value was the very last Incident we were looking at, with the description of ‘Need new Blackberry set up’, and so we got that outputted five times. gr in this case is kind of like that reference to the Caller name we talked about above — every time we did gr.next(), the Short description changed.

In order to fix this, we have to explicitly push the value of the data we want, and not just the reference of where to find the data.

ServiceNow has provided us a very helpful method within GlideRecord for this, called getValue(). With this method, the data will be translated to a string, and so when it is stored in the array it will no longer be a reference, but actual string data.

Let’s take a look at how the code will look when using getValue().

var array = [];

var gr = new GlideRecord('incident');
gr.addActiveQuery;
gr.setLimit(5);
gr.query();
while (gr.next()) {
array.push(gr.getValue('short_description')); // use getValue
}

for (item in array) {
gs.print(array[item]);
}

Now, when we run this…

*** Script: Unable to connect to email
*** Script: My computer is not detecting the headphone device
*** Script: Reset my password
*** Script: Need Oracle 10GR2 installed
*** Script: Need new Blackberry set up

Great! We have exactly what we wanted — rather than a reference to gr, which keeps changing, the actual value of the data was stored to the array each time we pushed.

In addition to getValue(), there’s another cool method called getDisplayValue(). Rather than getting the backend value for a field, it will get whatever is being displayed to the user.

For example, getValue() on a choice field will give the value, which may be a number, but getDisplayValue() will give the label.

--

--