UserCriteriaApi usage

ujjwala_678
Tera Contributor

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 😊

1 ACCEPTED SOLUTION

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

 

View solution in original post

9 REPLIES 9

Mark Manders
Mega Patron

Did you check on the User Criteria Diagnostics that is OOB available? 

Why do you need to check this from the portal? What is your business case for that? 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hello Mark,
Thank you for your inputs on this. I need to restrict portal widget access based on user's department hence trying to configure a user criteria which I can apply at the widget level.

Why not create a custom role, add that to a 'department group' where you automatically add/remove users based on the department and add that role to the widget. 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Chaitanya ILCR
Kilo Patron

Hi @ujjwala_678 ,

 

refer this

https://www.servicenow.com/community/developer-blog/usercriterialoader-api-to-evaluate-user-criteria...

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya