User Criteria Advanced Script to check if user has any role

jlaue
Kilo Sage

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!!

 

 

1 ACCEPTED SOLUTION

Nia McCash
Mega Sage
Mega Sage

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).

View solution in original post

6 REPLIES 6

getRoles() returns an object. You can try adding a toString()

if ((gs.getUser().getRoles().toString() != '')) {

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;
}