1

I'm trying to push records from a query to an array, so that I can compare the vaules. My problem is that I only get the first value 3 times (if the query returns 3 records).

My code:

var array = [];
var gr = new GlideRecord('incident');
gr.addQuery('scanned_by', gs.getUserID());
gr.query();

while (gr.next()) {
  array.push(gr.room);
}

gs.info(array);

The result:

a48d92241bafcc108ddd31d8cd4bcb99,a48d92241bafcc108ddd31d8cd4bcb99,a48d92241bafcc108ddd31d8cd4bcb9
6
  • 1
    What happens if you do array.push(gr.room.toString())? Commented May 27, 2020 at 13:44
  • Maybe adding gr = gr.next() after push statement will help? Commented May 27, 2020 at 13:45
  • 1
    How many records are in your gr response? and did you verify the data in the response is not just duplicates? Commented May 27, 2020 at 13:51
  • @Mathyn, thanks! That solved it. Commented May 27, 2020 at 13:52
  • It would be nice to know why this is needed. It looks to me like gr recycles objects to save on garbage collection, and gr.next() just returns the same object each time but mutates it so its string is different. I would hope that this behaviour is documented somewhere, because OP shouldn't be punished for writing perfectly idiomatic-looking code. Commented May 27, 2020 at 14:15

4 Answers 4

3

When you write array.push(gr.room) you are pushing a GlideElement onto an array. A GlideElement is an object. So what you are actually doing is pushing the memory address of that object onto your array.

Each time you go around the while loop the GlideElement gr.room will contain different content, but it is the same object with the same memory address. It would be very inefficient if ServiceNow were to allocate new memory for all the elements in a record every time a new record was read. When the loop ends, your array will contain a bunch of references to the same object, and that object will contain the last value that you read.

The method getValue returns a String, so when you write array.push(gr.getValue("room")) you are pushing a string value onto your array.

Here are several solutions that will work. What these solutions have in common is that they push a string value (and not a reusable object) onto the array.

  • array.push(gr.getValue("room"));
  • array.push(gr.room + "");
  • array.push(gr.room.toString());
  • array.push(String(gr.room));
Sign up to request clarification or add additional context in comments.

Comments

2
var array = [];
var gr = new GlideRecord('incident');
gr.addQuery('scanned_by', gs.getUserID());
gr.query();

while (gr.next()) {
  array.push(gr.getDisplayValue('room'));
}

gs.info(array);

You can write array.push(gr.getDisplayValue('room')) instead of array.push(gr.room)

Comments

1

Instead of array.push(gr.room) try array.push(gr.getValue("room"))

1 Comment

Welcome to Stack Overflow. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues.
1

In palce of array.push(gr.room) use array.push(gr.getValue("room"))

Corrected Code:

var array = [];
var gr = new GlideRecord('incident');
gr.addQuery('scanned_by', gs.getUserID());
gr.query();

while (gr.next()) {
  array.push(gr.getValue("room"));
}

gs.info(array);

array.push(gr.room) is pushing memory address of object that's why you are getting the same value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.