- 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:21 AM
Hi @Obito ,
It's giving proper value. if not workng for then try changing
-------------------------------------------------------------------------
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:23 AM
hi @Runjay Patel , The same value is pushing for 6 times. If you can please check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2024 03:28 AM
log out grUpdateApprover.user at the start of each loop, before you push the value to the array.
- 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
-------------------------------------------------------------------------