Auto-add users to group if the user is added to a certain role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 01:08 PM
Hello Team,
I have requirement that when a user is added to a role then they should automatically be added to a group
Example: If xyz user is added to "knowledge" role then xyz should be added to "KM Dashboard" group.
Can you help how can I achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 03:01 PM
Hi,
That sounds backward from OOB behavior. Roles are assigned to Groups, and when users are assigned to a Group they inherit the group roles. However, if you want to do what you state, you can achieve that via a Business Rule. But must define what group to use for each role.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 02:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 07:33 AM - edited 04-26-2023 12:36 PM
Here is the logic to add users to 'KM Dashboard' group when the 'knowledge' role is added to a user (record is created in the sys_user_has_role table).
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var roleName = current.role.getDisplayValue();
var userID = current.user;
gs.addInfoMessage("User: " + userID.getDisplayValue() + " now has role: " + roleName);
// logic to add user to group if assigned the knowledge role. Repeat for each role and group desired
if (roleName == 'knowledge') {
var grpMember = new GlideRecord('sys_user_grmember');
grpMember.addQuery('user', userID.toString());
grpMember.addQuery('group', 'KM Dashboard');
grpMember.query();
gs.addInfoMessage("Found " + grpMember.getRowCount() + " records for user: " + userID + " and group KM Dashboard");
if (grpMember.getRowCount() == 0) {
gs.addInfoMessage("Will add " + userID + " to group KM Dashboard");
grpMember.initialize();
grpMember.user = userID;
grpMember.group = 'f9820f3fdbd2a110cbb0651e13961972'; // sys_id of the sys_uer_group record for KM Dashboard, may be different in the instance.
grpMember.insert();
}
}
})(current, previous);
The business rule is defined on the 'sys_user_has_role' table, runs 'After' and on "insert' and 'Update'. See screenshot:
The result of my test follows:
This is not advised to do if you have the "Contextual Security: Role Management" and "Contextual Security: Role management V2" plugins (as most instances have). As this will break 'Role inheritance'. Also, you may want logic for when the 'knowledge' role is removed from a user to remove the user from the 'KM Dashboard' group.
I'm having trouble searching the docs site for related documentation. But I encourage you to do that, and evaluate current behavior around assigning users to groups, assigning roles to users, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2023 11:43 AM
Hi @Bert_c1
Thank you so much for the detailed explanation. I forgot to mention about one more requirement that is to check if the user is of a certain Business Unit and in this case if the user is from KPOE Business unit then only he should be added.
Also, can you please give me the code without the display and info message and those are not the requirement currently.
Appreciate your help on this. 🙂