GlideRecord only returning 1 record when I know there are more.

Markell
Tera Guru

Hi Guys

 

I created a GlideAjax and in the script Include I created a GlideRecord to give me all the groups that a user is in.

 

find_real_file.png

 

Now simply put, I get back user is in 2 groups, but in the gs.info log of the group name(s) it will only ever give me 1 group name and its the same if I just do groupGR.group (I only get back 1 sys_id. The sys_id of the first group in the list).

find_real_file.png

I feel like this is something I've done thousand times over and should get back all the records.

 

I have tried this code with while(groupGR.next())  also with no luck.

 

Can anyone offer any insight?

 

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Update below line 

if(groupGr.next()) 

as

while(groupGr.next()) 

return statement should be outside the loop so that it completes the loop first and then return all the values at once.  

Also, in the while you need to push all the groups into an array and return that.

Define array outside the while loop

Sample code

var arr = [];

while(groupGr.next()){ 
   arr.push(groupGr.group.toString());
}

return arr;

if returns only one record. While loop through all the records returned by the query. 

Regards,
Muhammad

View solution in original post

4 REPLIES 4

vkachineni
Mega Sage

that return should be out of the loop and use while()

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

MrMuhammad
Giga Sage

Update below line 

if(groupGr.next()) 

as

while(groupGr.next()) 

return statement should be outside the loop so that it completes the loop first and then return all the values at once.  

Also, in the while you need to push all the groups into an array and return that.

Define array outside the while loop

Sample code

var arr = [];

while(groupGr.next()){ 
   arr.push(groupGr.group.toString());
}

return arr;

if returns only one record. While loop through all the records returned by the query. 

Regards,
Muhammad

All of the responses were helpful and technically correct but I've marked this the correct answer as you went into the most detail to explain. Thanks

Pooja Mallikarj
Kilo Sage

Hi,

Try with below updated code.

var grp=[];
var gr=new GlideRecord('sys_user_grmember');
gr.addQuery('user','6816f79cc0a8016401c5a33be04be441'); //replace sys_id with user in your case 
gr.query();
while(gr.next())
{
grp.push(gr.group.name);
}
gs.print(grp);
return grp;

Thanks,

Pooja M