While loop is only pushing 1st value inside array

Obito
Tera Expert

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.

2 ACCEPTED SOLUTIONS

Hi @Obito ,

 

As i suggested above to use " arr.push(grUpdateApprover.getValue('user')); ". This will give you proper result.

 

RunjayPatel_0-1733311694095.png

 

-------------------------------------------------------------------------

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

-------------------------------------------------------------------------

 

View solution in original post

If you have basic knowledge in ServiceNow and face difficulties writing script in ServiceNow then this ServiceNow scripting tutorial is for you. In this tutorial i have explained from very basic to advance level of scripting. I have used real word use case to explain the scenarios. After watching

Juhi Poddar
Kilo Patron

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:

JuhiPoddar_0-1733311955301.png

 

 

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

View solution in original post

6 REPLIES 6

Runjay Patel
Giga Sage

Hi @Obito ,

 

It's giving proper value. if not workng for then try changing 

 arr.push(grUpdateApprover.getValue('user')); and then try.
 

RunjayPatel_0-1733311141092.png

 

-------------------------------------------------------------------------

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

-------------------------------------------------------------------------

 

 

 

hi @Runjay Patel , The same value is pushing for 6 times. If you can please check.

log out grUpdateApprover.user at the start of each loop, before you push the value to the array.

Hi @Obito ,

 

As i suggested above to use " arr.push(grUpdateApprover.getValue('user')); ". This will give you proper result.

 

RunjayPatel_0-1733311694095.png

 

-------------------------------------------------------------------------

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

-------------------------------------------------------------------------

 

If you have basic knowledge in ServiceNow and face difficulties writing script in ServiceNow then this ServiceNow scripting tutorial is for you. In this tutorial i have explained from very basic to advance level of scripting. I have used real word use case to explain the scenarios. After watching