- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 07:47 AM
Hello - I am working on creating a User Criteria record that checks if the logged in user has any role at all. I have been through many Community posts that check if a user has a specific role (such as ITIL), but that can be accomplished just using the form options on the User Criteria record. I am looking for help on User Criteria advanced script that checks to see if the user has any role at all, regardless of the role and then allow them to view the catalog item if they do have any role.
Thanks!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 08:38 AM
A couple of options...
You can check that gs.getUser().getRoles() is empty. See https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/GUserAPI#GUser-getRoles?...
Or, you can query the sys_user_has_role table for the user and check there are no results returned (ie. there are no entries in the sys_user_has_role for the user meaning that the user has no roles).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 09:14 AM
getRoles() returns an object. You can try adding a toString()
if ((gs.getUser().getRoles().toString() != '')) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2023 09:18 AM
I see what I did wrong there. I was able to get it working with this script:
if(gs.getUser().hasRoles()){
answer = true;
} else {
answer = false;
}