Check role assignments inside a workflow.

NeilH2
Giga Guru

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?

1 ACCEPTED SOLUTION

nikita_mironov
Kilo Guru

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.


View solution in original post

9 REPLIES 9

Yep that's working

This one is going in my code library

Thanks Nikita


Hi Nikita,



I am trying to use your code but it is throwing error in workflow.


"Illegal attempt to access class 'com.glide.sys.User' via script"




how can i resolve this error and can check the the user role in workflow.




Thanks


Shashank


If you are on a version after Dublin, then you will need to remove the package call from the script, replacing it with:



var myUserObj = new GlideUser.getUserByID(myUserSys_id);



Best regards,
David


Alessandro D_
Tera Contributor

Hi Neil,



why not using a GlideRecord query on the sys_user_has_role table?


Easier to implement and doesn't require to use objects and methods that might get deprecated at any point.



Thanks,


Ale


morrix7
Kilo Contributor

This did it for me.

 

// This script needs to set answer to 'yes' or 'no' to indicate the state of the activity.

answer = ifScript();

function ifScript() {
var myUserSys_id = current.variable_pool.requested_for.sys_id;
var myUserObj = user.getUserByID(myUserSys_id);
var userRole = myUserObj.hasRole('itil');

if (userRole) {


return 'yes';
}

return 'no';
}