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

Nitesh A
Tera Expert

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

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.  


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.

Hi,

Thanks a lot! ill test this solution 🙂