admin管理员组

文章数量:1415145

I'm trying to push records from a query to an array, so that I can pare 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

I'm trying to push records from a query to an array, so that I can pare 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
Share Improve this question edited May 27, 2020 at 13:44 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked May 27, 2020 at 13:41 JonfoJonfo 311 silver badge9 bronze badges 6
  • 1 What happens if you do array.push(gr.room.toString())? – Mathyn Commented May 27, 2020 at 13:44
  • Maybe adding gr = gr.next() after push statement will help? – chiragrtr 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? – edjm Commented May 27, 2020 at 13:51
  • @Mathyn, thanks! That solved it. – Jonfo 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. – David Knipe Commented May 27, 2020 at 14:15
 |  Show 1 more ment

4 Answers 4

Reset to default 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 mon 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));
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)

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

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.

本文标签: javascriptOnly first value is added when using arraypush in a while loopStack Overflow