- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:11 AM
While loop is only pushing 1st value inside array
var arr=[];
function test() {
var grUpdateApprover = new GlideRecord('sys_user_grmember');
grUpdateApprover.addEncodedQuery('group=0a52d3dcd7011200f2d224837e6103f2');
grUpdateApprover.query();
while (grUpdateApprover.next()) {
arr.push(grUpdateApprover.user);
}
gs.info(arr.toString());
}
test();
I tested this in background script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:28 AM
Hi @Obito ,
As i suggested above to use " arr.push(grUpdateApprover.getValue('user')); ". This will give you proper result.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:30 AM - edited 12-04-2024 03:32 AM
Hello @Obito
Script required only one minor change, convert the data to string before push into array.
Updated script:
var arr=[];
function test() {
var grUpdateApprover = new GlideRecord('sys_user_grmember');
grUpdateApprover.addEncodedQuery('group=0a52d3dcd7011200f2d224837e6103f2');
grUpdateApprover.query();
while (grUpdateApprover.next()) {
arr.push(grUpdateApprover.user.toString()); //updated line
}
gs.info(arr.toString());
}
test();
Result:
To understand the reason behind this, refer to my post in this thread Query is returning the same sys_id of the records
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:26 AM
Log out grUpdateApprover.getRowCount()
That way we can determine if problem is in the query or problem is in the loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 03:30 AM - edited 12-04-2024 03:32 AM
Hello @Obito
Script required only one minor change, convert the data to string before push into array.
Updated script:
var arr=[];
function test() {
var grUpdateApprover = new GlideRecord('sys_user_grmember');
grUpdateApprover.addEncodedQuery('group=0a52d3dcd7011200f2d224837e6103f2');
grUpdateApprover.query();
while (grUpdateApprover.next()) {
arr.push(grUpdateApprover.user.toString()); //updated line
}
gs.info(arr.toString());
}
test();
Result:
To understand the reason behind this, refer to my post in this thread Query is returning the same sys_id of the records
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar