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

Mark Stanger
Giga Sage

Try this instead...

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


That's worked perfectly, though I don't understand why!

Thanks a lot Mark.


Think of "countryL3.sys_id" as a point to GlideRecord object's sys_id property. When you push this to an array (or set a variable value) you are pushing the pointer, not the actual value. As you enumerate through the collection of GlideRecords the pointer changes until it's finally at the last record, and you have an array with a bunch of pointers to the same object and property. Using toString() or countryL3.getUniqueValue() or countryL3.getValue("sys_id") force the value to a string instead of an object/pointer.


Hello John,



Your explanation really helped me, but i have one doubt on the same.


when i use "gr.caller_id.sys_id" its working fine but getting only one record when i use "gr.sys_id" while pushing it in the array. I understand from the the above your explanation that we have to use "toString()" to push "gr.sys_id".



Could you please help me out with my doubt why i am getting all records in the array while pushing "gr.caller_id.sys_id".