Advanced user criteria - trying to restrict catalog item to users with specific company, and without ITIL role

Chase Stevenson
Mega Guru

Hello, I have an advanced user criteria that is set up to restrict certain users from seeing a catalog item in the Service Catalog.

Here's the script:

var company = gs.getUser().getCompanyID();

if(!gs.hasRole('itil') && company == "TEST"); {

answer = false;

}

What I can't get to work is the user role portion. I've tried (!gs.getUser().hasRole('itil') and that still doesn't work.

There are users in "TEST" company that have ITIL role, and they need to be able to view the catalog item. How can I restrict this to non-itil TEST company users?

1 ACCEPTED SOLUTION

Also you need to either wrap the other answer statement (answer = false) in an else statement or place it above the if statement as it is going to always return false.

 

var company = gs.getUser().getCompanyID();

if(!gs.hasRole('itil') && company == 'TEST'){

 answer = true;

}else{
	
 answer = false;
	
}

 

or

 

answer = false;

var company = gs.getUser().getCompanyID();

if(!gs.hasRole('itil') && company == 'TEST'){

 answer = true;

}

View solution in original post

21 REPLIES 21

gs.haRole will always takes the current in user. If admin impersonate non itil user it will return false

That is incorrect @dvp please see the documentation for gs.hasRole(). This returns true for users with the role and if an admin user is impersonating someone: https://docs.servicenow.com/bundle/jakarta-application-development/page/app-store/dev_portal/API_reference/glideSystemScoped/concept/c_GlideSystemScopedAPI.html There are also serveral community posts on the subject. https://community.servicenow.com/community?id=community_question&sys_id=7e570769db1cdbc01dcaf3231f961944 This was a "gotcha" for myself so now that I am aware I am able to properly code and test these types of scenarios.