Scripted User Criteria
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 02:28 PM
Hello,
I have a catalog item that we want to conditionally display based on a related table that contains emergency records. So we want the cat item to be hidden whenever there are no active emergencies, but appear when there are active emergencies. I was attempting to use the scripted user criteria for this to look up the emergency records and find if there are any active emergencies. Below is my script:
checkEvents();
function checkEvents() {
var queryStr = 'u_active=true';
var activeEvents = new GlideRecord('sn_customerservice_events');
activeEvents.addEncodedQuery(queryStr);
if (activeEvents.getRowCount() > 0) {
return false;
} else {
return true;
}
}
I added this user criteria to the 'Not available for' related record on the catalog item. It might be worth noting that the emergency event table is within the 'sn_customerservice_events' table, which is in the scoped CSM app, but the user criteria is global. I don't know if that could be throwing things off? I didn't see a way to create user criteria in the CSM scope.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 05:48 PM - edited 03-09-2023 05:48 PM
Hi,
So are you saying you've done user criteria diagnostics and it isn't working?
Obviously by you posting the question you must having an issue, but you didn't directly say if you were or not or if it was working for some or none, etc.
Anyways, is the query really using a field called "u_active"? not "active"? And your table name is "sn_customerservice_events"?
Your GlideRecord query isn't fully setup as you're missing:
activeEvents.query(); after the addEncodedQuery.
Please try reviewing your script again documentation such as: https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideRecordScoped...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 04:50 AM
Hello Allen,
Thanks for the response. I have tried several variations of syntax, so I must have forgotten to put the query statement back in. This is what it looks like currently:
checkEvents();
function checkEvents() {
var queryStr = 'u_active=true';
var activeEvents = new GlideRecord('sn_customerservice_events');
activeEvents.addEncodedQuery(queryStr);
activeEvents.query();
if (activeEvents.getRowCount() > 0) {
return false;
} else {
return true;
}
}
You are correct, the active field is actually a custom field called 'u_active'. There is no OOB active field on the 'sn_customerservice_events' table. I have tried it using user criteria diagnostics, but it doesn't seem to be giving consistent results. I have read in other user criteria threads that scripted criteria are not cached and that can sometimes lead to inconsistent results with the diagnostics.
Just to elaborate though, the availability really has nothing to do with any particular user info. It is completely dependent on the active status of the event records themselves. So the intent of the getRowCount is to determine if any of the events are active. If there is at least 1 active event, then the catalog item should be visible. However, if all of the events are inactive, the catalog item should be hidden.
This isn't the traditional use for user criteria that I've used before, but it seems like you should be able to do a query on a related table and return true/false based on those criteria just the same. However, other than the diagnostics tool, it doesn't seem like I'm able to even get logging statements to appear in the system log to troubleshoot further. If I put a gs.info statement on the first line it never even shows up in the system log.