Business Rule to remove User from a group

Rukayat I
Tera Contributor

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.

1 ACCEPTED SOLUTION

harshav
Tera Guru

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();
    }
}	

 

 

View solution in original post

4 REPLIES 4

harshav
Tera Guru

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();
    }
}	

 

 

What about if I want to add the snc_internal role when the Snc external group is deleted?

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();
        }
    }
}

 

This worked, thank you.