The CreatorCon Call for Content is officially open! Get started here.

Before adding the user to the group, need to check the user's role.

Siva82
Tera Expert

Hi Team,

We have a role called "Internal Role" assigned to users, and some groups have the "External Role" assigned.

For example, if I need to add a user to any group that has the "External Role," the "Internal Role" must be removed from the user’s account.

I need to achieve this functionality via a business rule. Could you please help me with this requirement?

Thank you in advance.

Best regards,
Siva

1 ACCEPTED SOLUTION

@Siva82 

your BR should be before insert on sys_user_grmember

Condition as: Group == Group Name

function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var usr = current.getValue('user');
    var role = 'sn_customerservice.customer';
    var usrrole = new GlideRecord('sys_user_has_role');
    usrrole.addQuery('user', usr);
    usrrole.addQuery('role.name', role);
    usrrole.query();
    if (usrrole.next()) {
        usrrole.deleteRecord();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Siva82 

usually we should assign users to groups and groups should have role.

If you add user to group 2(give external role) you can remove that user from group 1(give internal role)

BR: After insert on sys_user_grmember

What script did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

Hi @Ankur Bawiskar,

Thank you for your response. The "Internal Role" was assigned by default to every user in the user table. If I need to add a user to a group with the "External Role," the user cannot be added due to the restrictions imposed by the "Internal Role" in the user table. Therefore, in order to add a user to any group with the "External Role," the "Internal Role" must first be removed from the user’s account.

 

I tried Before BR, But no luck

function executeRule(current, previous /*null when async*/) {
 
// Add your code here
var usr = current.previous.user;
var rol = 'sn_customerservice.customer';
gs.log('user 1'+ user)
var usrrole = new GlideRecord('sys_user_has_role');
usrrole.addQuery('user', usr);
usrrole.addQuery('role', rol);
usrrole.query();
if(usrrole.next()){
usrrole.deleteRecord();
gs.log('user 2'+ user + 'role' + usrrole.role)
 
 
}
 
})(current, previous);
 
Thank you
Siva

@Siva82 

your BR should be before insert on sys_user_grmember

Condition as: Group == Group Name

function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var usr = current.getValue('user');
    var role = 'sn_customerservice.customer';
    var usrrole = new GlideRecord('sys_user_has_role');
    usrrole.addQuery('user', usr);
    usrrole.addQuery('role.name', role);
    usrrole.query();
    if (usrrole.next()) {
        usrrole.deleteRecord();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader