Catalog item Available For user criteria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 08:58 PM
Hello Experts,
I have a requirement to make catalog item available from portal, if logged in user has role 'itil' and belong to a specific business name (which is defined through a custom string type field in user table)
I have defined the catalog item Available for as below, however it still displays for any logged in user
Script:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 09:07 PM
Hi there,
Do follow whats mentioned in the default value of User Critera. You are ignoring some of these best practices.
/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or `gs.getUserID()`,
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 09:08 PM
Apart from the best practices, have you tried running your code in background script? And applying some debugging? Up to where does your code work? From where is it failing? Is your check on userRec.u_business valid or is this a reference while you are checking on a display value?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 09:09 PM
Also move the userRec.active check to the GlideRecord query, thats more efficient.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 09:37 PM
Hi @Mark Roethof ,
Thankyou for your replies.
I did try using the 'user_id' in my script as below and prior using gs.getUserID(), but received the
"user_id" is not defined
getBizValue(); function getBizValue() { var userRec = new GlideRecord('sys_user'); userRec.get(user_id); if(userRec.u_business=='Digital Solutions' && userRec.active == 'true' ) { answer= true; }
else
{
answer=false;
}
I did run it in background script.
The u_business custom field is a string type field.
Any help what I am doing wrong and to correct the script?
Thanks