Prevent to Add a user to a group, if this user belogns to another group

Facundo Prado
Tera Expert

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:

FacundoPrado_0-1698155594003.png


Any Idea if is this posible? Thanks

4 REPLIES 4

Tushar
Kilo Sage

Hi @Facundo Prado 

 

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

Tai Vu
Kilo Patron

Hi @Facundo Prado 

Let's try this.

1. Create a Before Insert Business Rule.

2. Set the conditions for all the groups that need to be validated.

Screenshot 2023-10-24 at 21.21.59.png

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

Screenshot 2023-10-24 at 21.34.25.png

(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

I´m trying to add people who belogns to "Requestor" groups, but no Error appears, and the people are added anyway.  

FacundoPrado_0-1698159085285.png

FacundoPrado_1-1698159088245.png
I´ve re-checked the bussRule, but it seems that everything is well typed.

Hi @Facundo Prado 

It works quite well from per my check.

TaiVu_0-1698159843910.png

 

Can you help to capture what have you done for the business rule for better understanding?