Script to check if user is part of existing group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 09:17 PM
Hi Team,
I have a requirement where I have 2 fields existing groups and add new group. In existing group I'm populating the groups which are already mapped to user. Whereas in the field Add New I need to populate the groups which are not mapped for the user and he should select only those groups.
Note both are of list collector variable types. Below is the script
Script Include :-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 10:36 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 10:42 PM
Your groups field, I hope is also list collector field referring to sys_user_group table.
It should work just fine
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 10:48 PM
Hello @Revathi12
Can you try like below :
Step 1 : Create Script include (Do not make client callable);
/*Get Groups which user is member of*/
isMemeberOfGroups: function() {
var userId = gs.getUserID();
var groups = [];
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user=' + userId);
grMem.query();
while (grMem.next()) {
groups.push(grMem.getValue('group'));
}
return 'sys_idIN' + groups.toString();
},
/*Get Groups which user is not member of*/
isNotMemeberOfGroups: function() {
var userId = gs.getUserID();
var groups = [];
var notPartOfGroups = [];
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user=' + userId);
grMem.query();
while (grMem.next()) {
groups.push(grMem.getValue('group'));
}
var grGroup = new GlideRecord('sys_user_group');
grGroup.addQuery('sys_idNOT IN' + groups.toString());
grGroup.query();
while (grGroup.next()) {
notPartOfGroups.push(grGroup.getUniqueValue());
}
return 'sys_idIN' + notPartOfGroups.toString();
},
Step 2 : Add reference qualifier in Variable 'Existing groups'
This will return the list of existing groups
javascript: new CheckGroupMembership().isMemeberOfGroups();
//Please see the screenshot for format
Step 3 : Add below reference qualifier in variable "New Groups"
javascript: new CheckGroupMembership().isNotMemeberOfGroups();
This will return the groups which user is not part of
Output :
For me Admin is only part of two groups
and not part of below groups
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 11:55 PM
Hi @Vishal
Thanks for the response. It is not working for existing groups, whatever the user I select it is showing the same groups.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 12:10 AM
Hi @Revathi12
Have you updated the same script include which you have previously used...??
Can you try to create new script include and then check....!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates