- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2012 03:31 AM
I am trying to setup an IF statement in a workflow to check if a user has an ITIL role applied to their user account. every time I run the statement it returns no even though the name_ref field contains a user with the ITIL role
I am getting the users name from a variable field on the catalog item called name_ref.
answer = ifScript();
var userrole = current.variable_pool.name_ref.hasrole('itil')
function ifScript() {
if (userrole)
{
return 'yes';
}
return 'no';
any ideas on what I am missing?
Solved! Go to Solution.
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2012 10:50 PM
Wiki Reference: http://wiki.servicenow.com/index.php?title=Getting_a_User_Object (and its Feedback section)
The defficulty here is that we are storing reference as GlideRecord but .hasRole method exists only is User object. So we will use getUserByID to initialize our user object from GlideRecord object using user's sys_id (or user_name, but I do not recommend that).
I assume that name_ref Record Producer/Catalog Item variable contains GlideRecord sys_user reference.
I think this should work:
answer = ifScript();
function ifScript() {
var myUserSys_id = current.variable_pool.name_ref.sys_id; // Getting sys_id from our user reference variable name_ref
var myUserObj = new Packages.com.glide.sys.User.getUserByID(myUserSys_id); // initializing user object variable with that user using getUserByID(myUserSys_id) method call, see wiki article above.
var userRole = myUserObj.hasRole('itil'); // executing method .hasRole over user object (but NOT GlideRecord reference!) that we had just initializied one line above;
if (userRole)
{
return 'yes';
}
return 'no';
Personally I started using this approach around three months ago and this allows me to eliminate the need of writing "own" functions to check user roles, group memberships, etc. - the main benefit here is that we are using OOB methods like .hasRole fully relying on ServiceNow OOB functionality.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2012 05:30 PM
I haven't tested this, but your JS is formatted incorrectly.
You're setting answer to the value returned by ifScript(), but ifScript has no idea was userrole is as you're defining it after running the method
Try this instead
answer = ifScript();
function ifScript() {
var userRole = current.variable_pool.name_ref.hasRole('itil');
if (userrole)
{
return 'yes';
}
return 'no';
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2012 12:44 AM
Just tried it and the workflow errored "Hasrole is not a function"
Now i've used that function elsewhere so i know it works

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2012 12:43 PM
I had a similar issue before with isMemberOf and found it's only available to gs or g_user
This might be another case of that where .hasRole() is only availble to gs or g_user
Aside from writing it out the long way by doing a GlideRecord lookup on sys_user_has_role, I don't know another way, unless someone else on the community wants to take a stab?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2012 10:50 PM
Wiki Reference: http://wiki.servicenow.com/index.php?title=Getting_a_User_Object (and its Feedback section)
The defficulty here is that we are storing reference as GlideRecord but .hasRole method exists only is User object. So we will use getUserByID to initialize our user object from GlideRecord object using user's sys_id (or user_name, but I do not recommend that).
I assume that name_ref Record Producer/Catalog Item variable contains GlideRecord sys_user reference.
I think this should work:
answer = ifScript();
function ifScript() {
var myUserSys_id = current.variable_pool.name_ref.sys_id; // Getting sys_id from our user reference variable name_ref
var myUserObj = new Packages.com.glide.sys.User.getUserByID(myUserSys_id); // initializing user object variable with that user using getUserByID(myUserSys_id) method call, see wiki article above.
var userRole = myUserObj.hasRole('itil'); // executing method .hasRole over user object (but NOT GlideRecord reference!) that we had just initializied one line above;
if (userRole)
{
return 'yes';
}
return 'no';
Personally I started using this approach around three months ago and this allows me to eliminate the need of writing "own" functions to check user roles, group memberships, etc. - the main benefit here is that we are using OOB methods like .hasRole fully relying on ServiceNow OOB functionality.