- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 12:29 AM
what is the best way to create a group in servicenow, that contains the users, that have the wanted company or wanted company.parent and for example license microsoft 365 E3 and are not part of some group x? i need to create separate user group for users that meet the set criteria.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 10:18 AM
Hi @Jori,
You can create a BR on sys_user_grmember table - before - Insert, with Condition is Group is 'Service Group 1'.
In the Advance tab, try below code:
(function executeRule(current, previous /*null when async*/ ) {
var user = new GlideRecord('sys_user');
if (user.get(current.user)) {
if (user.department != 'a581ab703710200044e0bfc8bcbe5de8' || !checkGroupMember(current)) {
current.setAbortAction(true);
}
}
})(current, previous);
function checkGroupMember(record) {
var member = new GlideRecord('sys_user_grmember');
member.addQuery('user', record.user);
member.query();
while (member.next()) {
if (member.group == 'b85d44954a3623120004689b2d5dd60a') { // CAB Approval
return false;
}
}
return true;
}
In this case, I will only allow to add group member with Department is Finance AND is not a member of 'CAB Approval' group, you can modify criteria of your choice.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 12:43 AM
Hello Jori,
I didn't get the full requirement. Can you please elaborate more please.
Are uses and groups already created and you want to add users to groups or users present just you need to create groups and update users in groups?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 12:52 AM
Hi,
users and groups and groups already exist in servicenow. i need to create new local group for example "service group 1" and i need to add members to this new group, that meet the set criteria. and the criteria are for example; company or company.parent is X, license is Y or Z and user does not belong in group N.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 10:18 AM
Hi @Jori,
You can create a BR on sys_user_grmember table - before - Insert, with Condition is Group is 'Service Group 1'.
In the Advance tab, try below code:
(function executeRule(current, previous /*null when async*/ ) {
var user = new GlideRecord('sys_user');
if (user.get(current.user)) {
if (user.department != 'a581ab703710200044e0bfc8bcbe5de8' || !checkGroupMember(current)) {
current.setAbortAction(true);
}
}
})(current, previous);
function checkGroupMember(record) {
var member = new GlideRecord('sys_user_grmember');
member.addQuery('user', record.user);
member.query();
while (member.next()) {
if (member.group == 'b85d44954a3623120004689b2d5dd60a') { // CAB Approval
return false;
}
}
return true;
}
In this case, I will only allow to add group member with Department is Finance AND is not a member of 'CAB Approval' group, you can modify criteria of your choice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2023 12:02 AM
Hi,
Thanks a lot! ill test this solution 🙂