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

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

Uncle Rob
Kilo Patron

Log out grUpdateApprover.getRowCount() 
That way we can determine if problem is in the query or problem is in the loop.

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