Sandeep Rajput
Tera Patron

@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+'');

View solution in original post