Dynamic member rule for group

Jori
Giga Guru

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.

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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.

View solution in original post

8 REPLIES 8

Hi,
tested the solution but no users appeared on the new test group.

i think this only works for new records but not for already existing ones?
how this can be modified to first run for already existing users if their company is for example x and they have group y but not group z then add user to group n?

Community Alums
Not applicable

Hi @Jori , the BR condition is only applied for 1 specific group as in your initial question. Please modify the group name as your choice or leave empty if you want to apply for all groups. 

 

This works for newly added member to the group since you wanted to only add members that meet the set criteria, if they don't, they will not be added.

 

And now you want to modify the existing members in the group, if they don't meet the set criteria then remove them? Please clarify more

hi, so the need is to add to this group all users from the portal that fit this criteria. both already existing users and the ones created in the future.

Community Alums
Not applicable

Hi, can you show me your BR because above BR already worked for any users that are added to the group mentioned in the BR's condition.