- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 11:44 AM
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.
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).
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?
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 11:47 AM
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.
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 11:47 AM
that return should be out of the loop and use while()
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 11:47 AM
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.
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2020 02:34 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2020 12:59 PM
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
