Business Rule for adding / removing 'xxx' role to assigned_to user

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi all,

I have the following requirements: 

  • If a new Incident will be "assigned to" a user that do not have a certain role "test", add the role automatically to the "assigned to" user
  • If an incident will be set in state Closed, remove automatically the role "test" to the "assigned to" user.

Could you please help me to do that? I was thinking to do it with a AFTER Business Rule.

Please any help are more than welcome!!!

Cheers

Alberto

1 ACCEPTED SOLUTION

Hi Alberto,

Just checked the API and hasRole is a glidesystem or glideuser method so we can't use it the way i've suggested! However, it's just occurred to me that you don't actually need it, the glide query on it's own will determine if the user has the role, if they don't it won't do anything, if they do it will delete it.

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
gr.query();
if(gr.next()){
gr.deleteRecord();
}

This does mean that the code that is adding the role isn't actually working properly though. You should change it to the below:

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
if(!gr.hasNext()){
gr.initialize();
gr.user = current.assigned_to;
gr.role = '282bf1fac6112285017366cb5f867469';
gr.insert();
}

View solution in original post

12 REPLIES 12

Hi Alberto,

Looks like i forgot to put in the query line in the code i gave you! Without that it's not actually running the queries we've defined! Hopefully this should work:

if(current.assigned_to.hasRole('test')){ 

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
gr.query();
if(gr.next()){
gr.deleteRecord();
}

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi Dave,

you're right thanks, however for some reasons it's still not working the BR 'remove', I tried with role and group table, same behavior 😞 let me try to share the setting of my BR, it must be something really easy to solve, 4 eyes are better than 2 🙂 thanks a lot.

find_real_file.png

find_real_file.png

Hi Alberto,

I can't see anything obvious wrong with the role, add in some debugging lines to help localise the issue:

gs.log('the BR has triggered properly');
gs.log('the user has the test role: ' + current.assigned_to.hasRole('test'));

if(current.assigned_to.hasRole('test')){ 

gs.log('im in the if statement now');

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
gr.query();

gs.log('the glide query has returned ' + gr.getRowCount() + ' records');

if(gr.next()){
gr.deleteRecord();

gs.log('the record has been deleted');

}

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi Dave,

adding some logs for debugging seems useful, this is the result:

the BR has triggered properly
the user has the test role: undefined

So, the problem is in the first IF condition, it's not able to do this check because the function hasRole() seems not working fine, any idea?

if(current.assigned_to.hasRole('test')){

Thanks a lot

Cheers

Alberto

Hi Alberto,

Just checked the API and hasRole is a glidesystem or glideuser method so we can't use it the way i've suggested! However, it's just occurred to me that you don't actually need it, the glide query on it's own will determine if the user has the role, if they don't it won't do anything, if they do it will delete it.

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
gr.query();
if(gr.next()){
gr.deleteRecord();
}

This does mean that the code that is adding the role isn't actually working properly though. You should change it to the below:

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
if(!gr.hasNext()){
gr.initialize();
gr.user = current.assigned_to;
gr.role = '282bf1fac6112285017366cb5f867469';
gr.insert();
}