Array push not working as expected in while loop

davidg3
Kilo Explorer

Hi,

I'm trying to do a very simple population of an array using a while loop, however the push seems to overwrite all previous values in the array. Any pointer much appreciated.

var countryLevel3 = [];
if (countryRef.u_organisation_level == 'Level 2') {
countryL3 = new GlideRecord('core_company');
countryL3.addQuery('u_reporting_level_2', countryRef.name);
countryL3.addQuery('u_organisation_level', 'Level 3');
countryL3.query();
while (countryL3.next()) {
countryLevel3.push(countryL3.sys_id);
var test = test + "," + countryL3.sys_id;
gs.addInfoMessage("Test: " + test);
for (x=0; x < countryLevel3.length; x++) {
gs.addInfoMessage(" countryname: " + countryLevel3[x]);
}

}

Example output is below. You can see the array is overwritten every loop, but if I just append to a string it works fine. Any ideas?

Test: 768b58b20a0aa021003602c67dba8f15
countryid in array: 768b58b20a0aa021003602c67dba8f15
Test: 768b58b20a0aa021003602c67dba8f15,768b58b60a0aa0210091365f325da49b
countryid in array: 768b58b60a0aa0210091365f325da49b
countryid in array: 768b58b60a0aa0210091365f325da49b
Test: 768b58b20a0aa021003602c67dba8f15,768b58b60a0aa0210091365f325da49b,768b58ba0a0aa0210125e4ea3dc6ea4a
countryid in array: 768b58ba0a0aa0210125e4ea3dc6ea4a
countryid in array: 768b58ba0a0aa0210125e4ea3dc6ea4a
countryid in array: 768b58ba0a0aa0210125e4ea3dc6ea4a

Even weirder, changing the while loop to output before and after:
var m=0;
while (countryL3.next()) {
var countryID = countryL3.sys_id;
gs.addInfoMessage("BEFORE. Loop#: " + m + " Level 2: "+ countryRef.name + " country: " + countryID + " countryarray: " + countryLevel3);
countryLevel3.push(countryID);
gs.addInfoMessage("AFTER. Loop#: " + m + " Level 2: " + countryRef.name + " country: " + countryID + " countryarray: " + countryLevel3);
m++;
}

The BEFORE value in the array has changed when the while loop does another iteration:
BEFORE. Loop#: 0 Level 2: Northern Europe (Nordics) country: 768b58b20a0aa021003602c67dba8f15 countryarray:
AFTER. Loop#: 0 Level 2: Northern Europe (Nordics) country: 768b58b20a0aa021003602c67dba8f15 countryarray: 768b58b20a0aa021003602c67dba8f15
BEFORE. Loop#: 1 Level 2: Northern Europe (Nordics) country: 768b58b60a0aa0210091365f325da49b countryarray: 768b58b60a0aa0210091365f325da49b
AFTER. Loop#: 1 Level 2: Northern Europe (Nordics) country: 768b58b60a0aa0210091365f325da49b countryarray: BEFORE. Loop#: 2 Level 2: Northern Europe (Nordics) country: 768b58ba0a0aa0210125e4ea3dc6ea4a countryarray: 768b58ba0a0aa0210125e4ea3dc6ea4a,768b58ba0a0aa0210125e4ea3dc6ea4a
AFTER. Loop#: 2 Level 2: Northern Europe (Nordics) country: 768b58ba0a0aa0210125e4ea3dc6ea4a countryarray: 768b58ba0a0aa0210125e4ea3dc6ea4a,768b58ba0a0aa0210125e4ea3dc6ea4a,768b58ba0a0aa0210125e4ea3dc6ea4a

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

Try this instead...

countryLevel3.push(countryL3.sys_id.toString());


View solution in original post

8 REPLIES 8

7 year old thread....still saving the day.

Just made my day a whole lot better 🙂

Thanks @Mark Stanger 

Old post but saved my day in 2021

Jim Coyne
Kilo Patron

I know this thread is old, but I thought this explanation may help - TNT: The Importance of Using "getValue()" when Getting Data from a GlideRecord