- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
I wanted to write a fix script that spawns test records.
On each test record I wanted to choose a random user record for a reference field.
So I created a GlideRecord object on the sys_user table queried all the active records, then went into a while loop pushing all the sys_id values to an array. With the intent of selecting a random index from that array.
every *single* sys_id in the array is the same value.
yet the value of sys_id is *different* if I log it before I push it into the array.
what the actual heck is going on here?
It's almost like GlideRecord is trying some mess to hide an asynchronous call from me and that abstraction is failing. If it's async can I please just await each query result?
here's my script. Attached screenshot shows the logging. note that the logging entries are also out of order -- I don't know if gs.info() is maybe asynchronous, or if the loop i'm inside of is asynchronous?
it can't be this hard. how is this happening? how do I just get an array of all the sys_id values?
Moreover how is it executing my second loop while it's still iterating through the first loop if this *isnt* asynchronous?
am so confused
gs.info(" === START === ");
var userList = [];
var c = new GlideRecordSecure('sys_user');
c.addQuery('active', true);
c.query();
while(c.next()) {
gs.info(`${c.sys_id} -> ${c.name}`);
userList.push(c.sys_id);
};
gs.info(`userList length: ${userList.length}`);
userList.forEach((a) => {gs.info(`list -> ${a}`)});
gs.info(" === END === ");
UPDATE @ 1256 CTD (still waiting on post approval)
I didn't figure it out. But I did figure out how to provoke the while loop to insert the *current* value of sys_id into the array rather than copying the same value into the array for every row.
// this works
while (c.next()){
let sys_id = c.getValue('sys_id');
userList.push(sys_id);
}
// this DOES NOT WORK
while (c.next()){
// how are these two values of c.sys_id different??!!
gs.info(c.sys_id); // logs a different value here
userList.push(c.sys_id); // pushes the value from the first time through the loop every time
}
seriously though.
understanding how on Earth this can happen is not trivia and not something I don't need to know.
I need to understand whats happening here. It's foundational.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Amy Hicox This is foundational.
// this works
while (c.next()){
let sys_id = c.getValue('sys_id');
userList.push(sys_id);
}
This works as c.getValue('sys_id') returns a string value of sys_id field which is pushed to the userList array.
// this DOES NOT WORK
while (c.next()){
// how are these two values of c.sys_id different??!!
gs.info(c.sys_id); // logs a different value here
userList.push(c.sys_id); // pushes the value from the first time through the loop every time
}
The code above doesn't work as it pushes the reference (and not the value) of c.sys_id to array. You’re pushing the GlideElement object itself into the array, not the string value.Later, when the loop advances, that object’s internal pointer changes, so every element in your array ends up pointing to the last record in the loop.
To overcome this issue you can use any of the following syntex.
userList.push(c.getValue('sys_id'));
or
userList.push(c.sys_id.toString());
or
userList.push(c.sys_id+'');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Amy Hicox This is foundational.
// this works
while (c.next()){
let sys_id = c.getValue('sys_id');
userList.push(sys_id);
}
This works as c.getValue('sys_id') returns a string value of sys_id field which is pushed to the userList array.
// this DOES NOT WORK
while (c.next()){
// how are these two values of c.sys_id different??!!
gs.info(c.sys_id); // logs a different value here
userList.push(c.sys_id); // pushes the value from the first time through the loop every time
}
The code above doesn't work as it pushes the reference (and not the value) of c.sys_id to array. You’re pushing the GlideElement object itself into the array, not the string value.Later, when the loop advances, that object’s internal pointer changes, so every element in your array ends up pointing to the last record in the loop.
To overcome this issue you can use any of the following syntex.
userList.push(c.getValue('sys_id'));
or
userList.push(c.sys_id.toString());
or
userList.push(c.sys_id+'');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thank you!
As a newcomer to this platform, how on Earth could I have ever been expected to know this? Is it written down somewhere? Like is there a document that actually explains how this is supposed to work? I see the API docs for GlideRecord but no details on how it actually works are given, just very brief descriptions and code-snippets?
Did I miss the correct documentation somewhere?