
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-23-2019 11:41 AM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 05:19 PM
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;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 07:28 AM
I just tested in my instance and it working as expected.
Can you try with a new user?
Clear the cache for previously tested user by cache.do in application navigator for impersonated user and try again

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 07:42 AM
I cleared cached with cache.do and tried a different user. I still get the same result.
1.) I have the user criteria under the catalog item's "not available for" related list:
2.) The user criteria has advanced script of the following:
3.) I have two users in TEST company, one with ITIL role, and one with NO roles:
4.) When ITIL user goes to Service Catalog, they can see the catalog item.
5.) When user with NO roles goes to Service Catalog, they can see the catalog item.
The script should return 'true' if user does NOT have itil role AND user is in 'test' company (which is valid for user #2 who has NO roles, and is a member of 'test' company). For some reason, the script is NOT returning 'true' as it is not recognizing that user #2 is not an itil role user.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 09:17 AM
Can you try with navigating to url directly as below
Update the item sys_id
https://dev1xxxxx.service-now.com/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=e212a942c0a80165008313c59764eea1
I did the similar testing as described above but navigated directly to url.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 11:33 AM
I replaced the instance and sys_id in the URL with my own, and can confirm that both ITIL and NON-ITIL TEST company users are still able to see the catalog item.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2019 10:56 AM
When you impersonate someone gs.hasRole('WHATEVER') will always return true as you yourself have the Admin role. You need to either fully log in as that user (not impersonate) to test or you also have to check !hasRole('admin') in the script.