Catalog item Available For user criteria

somujit
Tera Contributor

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

somujit_0-1721965942579.png

Script:

getBizValue();
function getBizValue() {
    var userID = gs.getUserID();
    var userRec = new GlideRecord('sys_user');
    userRec.get(userID);
    if(userRec.u_business=='Digital Solutions' && userRec.active == 'true' ) {
       answer= true;
    }
else
{
 answer=false;
}
}
 
Can you please advise what I am doing wrong here and how to fix this issue?
 
Thanks and Regards,
Somujit
7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

somujit
Tera Contributor

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