g_user.hasRoles question

servicetrout
Tera Expert

Running code below in a client script on form, not on a record producer.  The user has no Department or Division role, but does have a Unit role.

alert ("Starting");
var gUser = g_user.userID;
var Reviewer = g_user.hasRoles('Department','Division');
var personalRole = g_user.hasRole('Unit');
alert ("User: " + gUser +  " Reviewer: " + Reviewer + "  Unit Role :" + personalRole);

The last alert is showing the correct user (when I impersonate them). 

Reviewer is ALWAYS true, even though the user doesn't have those roles.

The personalRole is correct and has been tested for both true and false conditions.  

 Any idea why it isn't returning false for Reviewer?

 

 I've confirmed the user doesn't have either role (twice). 

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

g_user.hasRoles(); I believe it evaluates if current user has ANY role whether it's 'Department','Division' or ANY other role. If user has any role other than 'Department','Division' roles it should return true.

find_real_file.png

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Reference https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

g_user.hasRoles()True if the current user has at least one role specified, false otherwise.

Usage: g_user.hasRoles('itil','admin')

 

Are you sure, user doesn't have any one role of Department or Division?

Also try using g_user.hasRole instead of g_user.hasRoles to verify those roles individually.

 


Please mark this response as correct or helpful if it assisted you with your question.

Omender Singh
Tera Guru

You can watch this video to get 100% working of function.