Prevent to Add a user to a group, if this user belogns to another group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 06:53 AM
Hello everyone! I have some groups, and I need to prevent add to a Group to all users who belons to the REQUESTER groups.
Groups:
Any Idea if is this posible? Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 07:29 AM
Please modify below BR as per your requirement -
(function executeRule(current, previous) {
// Get the name of the group that the user is being added to.
var groupName = current.getValue('group');
// Check if the user is already a member of a group that contains the string "REQUESTER."
var user = current.user.toString(); // Assuming "user" is the reference field to the user in the group record.
// Create a GlideRecord query to fetch the user's group memberships.
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user);
grMember.query();
while (grMember.next()) {
var existingGroup = grMember.group.name;
if (existingGroup.indexOf('REQUESTER') >= 0) {
// The user is already a member of a group that contains the string "REQUESTER," so prevent them from being added to the new group.
gs.addErrorMessage('The user is already a member of a group that contains the string "REQUESTER".');
current.setAbortAction(true);
return;
}
}
})(current, previous);
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 07:35 AM
Let's try this.
1. Create a Before Insert Business Rule.
2. Set the conditions for all the groups that need to be validated.
3. In Advanced tab, check if the new member of the validated groups is a member of MPR_Requestors group, then abort the action.
Sample below
(function executeRule(current, previous /*null when async*/ ) {
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', 'eabc4702478635504fb0f9cf016d430e'); //MPR_Requestors
grMember.addQuery('user', current.getValue('user'));
grMember.setLimit(1);
grMember.query();
if (grMember.hasNext()) {
current.setAbortAction(true);
}
})(current, previous);
let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 07:53 AM
I´m trying to add people who belogns to "Requestor" groups, but no Error appears, and the people are added anyway.
I´ve re-checked the bussRule, but it seems that everything is well typed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 08:04 AM
It works quite well from per my check.
Can you help to capture what have you done for the business rule for better understanding?