
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 02:38 AM
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
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 03:42 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 06:57 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2018 06:06 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2018 12:26 AM
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');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 02:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2018 03:42 AM
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();
}