- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 09:56 AM
The default when users are added from AD is to add them to the SNC External Customer group. I am trying to write a business rule that would remove a user from SNC External customer group when they are added to a new group.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 01:19 PM - edited ‎11-14-2023 01:23 PM
Hi,
You can write an after insert business rule on the table 'sys_user_grmember' table. if the user is added in to other group we are checking that user is part of your intended group or not and we will remove that user. also add a condition user is not empty and group is not empty.
var user = current.user.sys_id;
var groupName = current.group.name;
if (user && groupName != 'SNC External Customer') {
var query = 'group.name=SNC External Customer^user=' + current.user.sys_id; // here update your group name
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 01:19 PM - edited ‎11-14-2023 01:23 PM
Hi,
You can write an after insert business rule on the table 'sys_user_grmember' table. if the user is added in to other group we are checking that user is part of your intended group or not and we will remove that user. also add a condition user is not empty and group is not empty.
var user = current.user.sys_id;
var groupName = current.group.name;
if (user && groupName != 'SNC External Customer') {
var query = 'group.name=SNC External Customer^user=' + current.user.sys_id; // here update your group name
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 01:57 PM
What about if I want to add the snc_internal role when the Snc external group is deleted?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 02:50 PM - edited ‎11-14-2023 02:51 PM
Here is the code, change one line where i commented you to add the sys_id. Please add sys_id there.
var user = current.user.sys_id;
var groupName = current.group.name;
if (user && groupName != 'SNC External Customer') {
var query = 'group.name=SNC External Customer^user=' + current.user.sys_id; // here update your group name
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
gr.deleteRecord();
var roleQuery = 'role.name=snc_internal^user='+ user;
var role = new GlideRecord('sys_user_has_role');
role.addQuery(roleQuery);
role.query();
if(!role.next()){
role.initialize();
role.user = user;
role.role = ''//sys_id of your snc_internal role;
role.insert();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2023 03:05 PM
This worked, thank you.