
- 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 03:10 AM
Hi Alberto,
I'm not sure this is the best way to manage licenses but you can do it a couple of ways. It's generally considered best practice to assign roles to groups and then add users to groups so they inherit the roles, it just makes administration a bit easier.
If you do that you can create a group with the role you need and then after an incident is created and assigned, run a check to see if the assignee as the required role and, if they don't add them to the group.
if(!current.assigned_to.hasRole('your_role')){
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.user = current.assigned_to;
gr.group = 'sys_id of the group you made';
gr.insert();
}
Then you can have another after update rule running when the incident closes to find that record and delete it.
The other option is to add the role directly in the sys_user_has_role table, it's the same logic, just inserting a record into a different table.
Cheers
Dave

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 04:55 AM
Hi Dave,
thanks a lot for your answer.
I was able to insert the role using your idea but on the sys_user_has_role table, now I'm trying to delete the role when the incident goes in state Resolved but it's not working, I'm using the following code:
if(current.assigned_to.hasRole('test')){
var gr = new GlideRecord('sys_user_has_role');
gr.initialize();
gr.user = current.assigned_to;
gr.role = '282bf1fac6112285017366cb5f867469';
gr.deleteRecord();
}
Do you have any idea the reason why is not working properly?
Thanks
Alberto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 05:22 AM
Hi Alberto,
initialize() is only used when inserting new records, to delete you have to use a normal glide record query to find the record you need and then delete it:
if(current.assigned_to.hasRole('test')){
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.assigned_to);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469');
if(gr.next()){
gr.deleteRecord();
}
Cheers
Dave

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2018 06:43 AM
Hi Dave,
thanks, it's not working..really strange, it seems like nothing has been triggered by this Business Rule, I will take a look more in deep and let you know.
In the meanwhile, any idea of possible root cause, feel free to share me.
Cheers
Alberto