User Has Role?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2009 12:27 PM
I was writing a business rule to add a third party to the watch list of a task generated from a service catalog request item. "Simple", I said. Then I realized how many tickets our service desk agents open and I'm sure they don't want to be on the watch list for each one. I thought this was the answer:
if (current.request_item.request.opened_by.hasRole('itil')) ...
But Don Goodliffe told me that hasRole() only works on a gs (GlideSession) object (aka currently logged in user). Instead, to get my hands on someone already assigned to a field like opened_by, I would need to query in the sys_user_has_role table for the right info. He was also kind enough to provide me with a function. As a result, here is what I ultimately came up with for my business rule (run on sc_task as a BEFORE rule.)
function userHasRole(userID, role) {
var uhrRec = new GlideRecord('sys_user_has_role');
uhrRec.addQuery('user', userID);
uhrRec.addQuery('role.name', role);
uhrRec.query();
return uhrRec.hasNext();
}
var openedBy = current.request_item.request.opened_by;
if (!userHasRole(openedBy, 'itil')) {
var wList = current.watch_list;
wList = wList + "," + openedBy.name;
current.watch_list = wList;
}
- 22,705 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-06-2013 12:37 PM
Blair, good to hear from you.
Yeah, you'll have to query the sys_user_has_role table for the users in that group. A bit of looping, but not difficult. I wouldn't do this from a client script though unless something is changing and you need to know it dynamically. If you must, ensure you are using an asynchronous Ajax call, not synchronous to avoid "user hangs".
You could also use a Display business rule and put the results in the g_scratchpad to push them to the client as an alternate option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-15-2017 03:18 PM
Hello Chuck,
A requirement emerged for us that led me to this post. My audit team needs the ability for when they add users to the sn_audit_engagement form in a user created field: Need to Know Only (u_need_to_know_only) that they are automatically added to the sn_audit.privilege role in order to be able to see the privileged record and added Module item (Attorney-Client Privilege). This field is a list type which allows adding multiple people. Would you recommend that I use g_scratchpad?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 05:36 AM
Hi Rhett,
This definitely sounds like a business rule, but g_scratchpad is not the right mechanism here. You'll need to use an after Business Rule to determine who has been added (or removed) from that list field. It's a comma separate list of sys_ids which makes the lookup pretty easy using some GlideRecord script to update the sys_user_has_role table, where the assignment of users-roles is kept.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2013 04:16 PM
function usrHasRole(userID, role) {
var gsUser = gs.getUser().getUserByID(userID);
return gsUser.hasRole(role);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2017 08:19 AM
Also if you just want to see if the current user has ANY role at all (for ACLs or other conditions) you can use: gs.getUser().hasRole()
