How to get users names by comma seperated from script.

rahul16
Giga Guru

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.

1 ACCEPTED SOLUTION

Zach Koch
Giga Sage
Giga Sage

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);
If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

View solution in original post

3 REPLIES 3

Zach Koch
Giga Sage
Giga Sage

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);
If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

chrisperry
Giga Sage

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

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

H_9
Giga Guru

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.

Thanks. 🙂