- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 07:00 AM
Can anyone help me what is wrong with the script?
name = '558985e41b470550c8e7db9ebd4bcbb5,d294016c97c7c910522d74500153af86';
var users = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', name);
gr.query();
while (gr.next()){
users.push(gr.name);
}
gs.print(users);
Output:- test1,test2
but I am getting output as test2,test2.
Solved! Go to Solution.
- Labels:
-
Multiple Versions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 07:04 AM
You need to change your script to push the value, not the pointer to the value in your while loop.
name = '558985e41b470550c8e7db9ebd4bcbb5,d294016c97c7c910522d74500153af86';
var users = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', name);
gr.query();
while (gr.next()){
users.push(gr.getValue('name'));
}
gs.print(users);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 07:04 AM
You need to change your script to push the value, not the pointer to the value in your while loop.
name = '558985e41b470550c8e7db9ebd4bcbb5,d294016c97c7c910522d74500153af86';
var users = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', name);
gr.query();
while (gr.next()){
users.push(gr.getValue('name'));
}
gs.print(users);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 07:04 AM
Hi there,
You need to update the line of code from users.push(gr.name); to be either one of the two below to make sure you are passing the string value and not an object:
users.push(gr.name.toString());
users.push(gr.getValue('name'));
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 07:06 AM
Hello,
You have to add .toString() while adding it to the array.
There is one nice article explaining that why we have to use .toString() while adding to the array.
I will add that article in this thread once I find it again.
Meanwhile use following script, it will work. 🙂
name = '558985e41b470550c8e7db9ebd4bcbb5,d294016c97c7c910522d74500153af86';
var users = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', name);
gr.query();
while (gr.next()){
users.push(gr.name.toString());
}
gs.print(users);
Output:- test1,test2
Please mark the answer correct if it helps. 🙂
Thanks.