hasRole() says true for everything

grosch
Mega Expert

On one of my test instances where I'm logged in as admin so I can execute code in the "Scripts - Background" window, I'm seeing odd results related to roles.   If I run this:

 

var user = gs.getUser();
gs.print(user.getFullName());
gs.print(user.getFirstName());
gs.print(user.hasRole('There is no possible way this is a real role'));

 

I get the correct names printed, but then line 4 returns true.   Doesn't matter what I put there, it just always says true.   I'm trying to determine whether the current user has the Request Manager role in a specific service.

1 ACCEPTED SOLUTION

ohhgr
Kilo Sage
Kilo Sage

Hi Scott,



gs.hasRole() returns 'true' all the time for an Admin user. http://wiki.servicenow.com/index.php?title=GlideSystem#hasRole.28String.29



If you want to check for a role and make sure that an admin isn't looking at the record, you can use a condition like



gs.print(user.hasRole('some_role') && !user.hasRole('admin'));



Hope that helps.


Mandar


View solution in original post

2 REPLIES 2

ohhgr
Kilo Sage
Kilo Sage

Hi Scott,



gs.hasRole() returns 'true' all the time for an Admin user. http://wiki.servicenow.com/index.php?title=GlideSystem#hasRole.28String.29



If you want to check for a role and make sure that an admin isn't looking at the record, you can use a condition like



gs.print(user.hasRole('some_role') && !user.hasRole('admin'));



Hope that helps.


Mandar


As Mandar said, admin users have every role by default, even made up roles.   This was an issue for me when I was at an old employer and trying to restrict a field from everyone except for a set of users (accounting field).   In the end, accounting just had to accept that we could see the role and trust us not to do anything with the info.