Adding members with role to a group

Staxed
Giga Guru

Trying to figure out the best, or most efficient, way to do this.

We have multiple groups that share a specific role, and we'd like to make sure all of the people with that role are added to another group.  We don't want to add the groups themselves to this group.  Any suggestions on the best way to do this?

I'm thinking about Flow Designer, so I could have it scheduled to only run once a day at midnight (30k+ users to check), though I've been playing with it and having a hard time setting it up to find all the users with a specific role and then check to see if they are already in the group, and if not add them.

These users change on a fairly regular basis, so doing this as a business rule when a user is added won't work (though a business rule that only runs on role change might).

1 ACCEPTED SOLUTION

chrisperry
Giga Sage

Hi there,

This could be accomplished with Flow Designer for sure, or you could do it with a scheduled script execution, which is a bit easier/faster for me. The below script should meet your requirements (don't forget to update the sys_id of your role and group accordingly):

var userHasRoleGr = new GlideRecord('sys_user_has_role');
userHasRoleGr.addEncodedQuery(gs.getMessage('state=active^role={0}', 'role_sys_id_here')); //replace sys_id of your role here
userHasRoleGr.query();

while (userHasRoleGr.next()) {
    var groupMemberGr = new GlideRecord('sys_user_grmember');
    groupMemberGr.addEncodedQuery(gs.getMessage('group={0}^user={1}', ['group_sys_id_here', userHasRoleGr.getValue('user')])); //replace sys_id of your group that they should be a member of if they have the role
    groupMemberGr.query();
    if (!groupMemberGr.hasNext()) {
        var newGroupMemberGr = new GlideRecord('sys_user_grmember');
        newGroupMemberGr.initialize();
        newGroupMemberGr.setValue('group', 'group_sys_id_here'); //replace sys_id of your group that they should be a member of if they have the role
        newGroupMemberGr.setValue('user', userHasRoleGr.getValue('user'));
        newGroupMemberGr.insert();
    }
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

View solution in original post

2 REPLIES 2

chrisperry
Giga Sage

Hi there,

This could be accomplished with Flow Designer for sure, or you could do it with a scheduled script execution, which is a bit easier/faster for me. The below script should meet your requirements (don't forget to update the sys_id of your role and group accordingly):

var userHasRoleGr = new GlideRecord('sys_user_has_role');
userHasRoleGr.addEncodedQuery(gs.getMessage('state=active^role={0}', 'role_sys_id_here')); //replace sys_id of your role here
userHasRoleGr.query();

while (userHasRoleGr.next()) {
    var groupMemberGr = new GlideRecord('sys_user_grmember');
    groupMemberGr.addEncodedQuery(gs.getMessage('group={0}^user={1}', ['group_sys_id_here', userHasRoleGr.getValue('user')])); //replace sys_id of your group that they should be a member of if they have the role
    groupMemberGr.query();
    if (!groupMemberGr.hasNext()) {
        var newGroupMemberGr = new GlideRecord('sys_user_grmember');
        newGroupMemberGr.initialize();
        newGroupMemberGr.setValue('group', 'group_sys_id_here'); //replace sys_id of your group that they should be a member of if they have the role
        newGroupMemberGr.setValue('user', userHasRoleGr.getValue('user'));
        newGroupMemberGr.insert();
    }
}

If this answer is helpful please mark correct and helpful!

Regards,

Christopher Perry

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

this worked perfectly, thank you!