User is in multiple groups, but query only returns one group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 08:52 AM
I have a script include that needs to return the groups that a user is a member of. Currently my script inlcude is only returning one group, even if there are 3 groups total? Why is my query only getting one group?
var currentUser = this.getParameter('sysparm_user')
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', currentUser);
gr.query();
if (gr.next()) {
var groupname = [];
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id', gr.group);
group.query();
while(group.next()) {
groupname.push(group.name.toString();
}
return groupname.toString();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 09:01 AM
Hi,
Here's an example background script to help you with this. This is for the demo user Beth Anglin:
gs.info(userGroups("46d44a23a9fe19810012d100cca80666"));
function userGroups(currentUser){
var grMember = new GlideRecord("sys_user_grmember");
grMember.addQuery("user", currentUser);
grMember.query();
var groupArray = [];
while(grMember.next()){
groupArray.push(grMember.getDisplayValue("group"));
}
return groupArray.toString();
}
You will want to pass in the user parameter in where I've manually passed the sys_id.
The result of this script running for beth was:
NA Escalation Approvers,EMEA Escalation Approvers,Network CAB Managers,Hardware,Database,Software,Catalog Request Approvers for Sales,Service Desk
Also - to specifically answer your question, it was the if (gr.next) that was causing it, that needed to have been a while. You then do a second GlideRecord query which isn't required though.
Hope that helps!
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 09:32 AM
Thanks.
so do i not need this line?
var currentUser = this.getParameter('sysparm_user')
Where am i setting current user to be this parameter?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 09:52 AM
Also, in my catalog client script, what would be the correct way to set these values?
g_form.setValues('groups', what goes here?);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 06:30 AM
Please try as below
var currentUser = this.getParameter('sysparm_user');
userGroups(currentUser); // calling the function
function userGroups(currentUser){
var grMember = new GlideRecord("sys_user_grmember");
grMember.addQuery("user", currentUser);
grMember.query();
var groupArray = [];
while(grMember.next()){
groupArray.push(grMember.getDisplayValue("group"));
}
return groupArray.join(',');
}
in client script try to set the value
g_form.setValues('groups', the returned answer from the script include);