- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 02:40 AM
Hi everyone,
I am currently working on a requirement where I need to evaluate User Criteria for a Knowledge Base at the Service Portal widget level.
Here’s what I’m trying to achieve:
We have user criteria set up for different Knowledge Bases.
I want to check if the logged-in user has access to a specific KB article or Knowledge Base based on the configured user criteria.
This check needs to happen inside a Service Portal widget (server-side or client-controller if needed).
What I’m looking for:
Best practice to evaluate user criteria via UserCriteria API (or any recommended approach).
What methods or APIs should I be using — e.g.,
UserCriteriaLoader() ?
UserCriteriaEvaluator() ?
Any specific helper like sn_uc.UserCriteriaLoader.getAllUserCriteria()?
How to invoke it correctly in a widget context (server script or client call to Script Include?)
Example or snippet to check if current user has access to a KB article or base using user criteria
What I’ve tried so far:
I’ve looked into sn_uc.UserCriteriaLoader, but I’m not sure if it’s the right fit here.
Also wondering if I need a Script Include or if there's a simple method to evaluate access inline.
Would really appreciate any insights, examples, or pointers from anyone who has implemented something similar!
Thanks in advance 😊
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 05:41 AM
Hi @ujjwala_678 ,
add your user crietria sysid in the array in the 2nd line (I have added a dummy one for reference) just replace it your user criteria sysid
replace the user_id with sysid of the user in the last line
try this
make adjustments if required
function checkCondition(user_id) {
var userCriterias = ['6b6df061ff2121009b20ffffffffff44']; //replace the array with your user criteria sysids
var meetsCriteria = sn_uc.UserCriteriaLoader.userMatches(gs.getUserID(), userCriterias);
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', user_id);
userHasRole.addQuery('role.name', 'admin');
userHasRole.query();
if (meetsCriteria && userHasRole.hasNext()) {
gs.info("user found");
return true;
}
return false;
}
answer = checkCondition(user_id); //replace user id with sys id of the user
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 03:07 AM - edited 06-27-2025 03:08 AM
what did you start with and where are you stuck?
yes that API needs to be used in Server Side.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 05:27 AM - edited 06-27-2025 05:28 AM
Hello @Ankur Bawiskar ,
Thank you for your response on this.
I’ve been working on a server-side script to restrict user access at the widget level, but unfortunately, it’s not working as expected. Could you please help me identify where I might be going wrong or suggest the correct approach?
Appreciate your guidance on this.
answer=function checkCondition(user_id) {
var userCriteriaId = 'SYS_ID'; //sys_id of the user_criteria
var userCriteriaLoader = new SNC.UserCriteriaLoader();
var meetsCriteria = userCriteriaLoader.evaluate(userCriteriaId, user_id);
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', user_id);
userHasRole.addQuery('role.name', 'admin');
userHasRole.query();
if (meetsCriteria && userHasRole.hasNext()) {
gs.log("user found");
return true;
}
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 05:41 AM
Hi @ujjwala_678 ,
add your user crietria sysid in the array in the 2nd line (I have added a dummy one for reference) just replace it your user criteria sysid
replace the user_id with sysid of the user in the last line
try this
make adjustments if required
function checkCondition(user_id) {
var userCriterias = ['6b6df061ff2121009b20ffffffffff44']; //replace the array with your user criteria sysids
var meetsCriteria = sn_uc.UserCriteriaLoader.userMatches(gs.getUserID(), userCriterias);
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', user_id);
userHasRole.addQuery('role.name', 'admin');
userHasRole.query();
if (meetsCriteria && userHasRole.hasNext()) {
gs.info("user found");
return true;
}
return false;
}
answer = checkCondition(user_id); //replace user id with sys id of the user
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 05:45 AM
try this
function checkCondition(user_id) {
var userCriteriaId = 'SYS_ID'; //sys_id of the user_criteria
// this gives you array
var allCriterias = new sn_uc.UserCriteriaLoader.getAllUserCriteria(gs.getUserID());
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', user_id);
userHasRole.addQuery('role.name', 'admin');
userHasRole.query();
if (allCriterias.indexOf(userCriteriaId) > -1 && userHasRole.hasNext()) {
gs.info("user found");
return true;
}
return false;
}
answer = checkCondition(user_id);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 07:42 AM
Thank you for marking my response as helpful.
As per new community feature you can mark multiple responses as correct.
I believe my response is also correct and is another way to achieve the same.
If my response helped please mark it correct as well so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader