Advanced User criteria for service portal

Joe K2
Tera Contributor

I've read through of the few articles that said SOLVED, but have had zero luck making this work.

 

I need a user criteria for our service portal that evaluates if the user is a member of IT.  The problem is we don't have great HR data coming into ServiceNow so we have a custom field that collects it from Active Directly, but it collects a bunch of other information as well as the department.

 

So i have a custom field named u_hrdepartment in the user records that may contain Information Technology ... among other things to define if the user is IT or not.

 

Attempt 1

 

var answer = checkCondition();

// Define the field and value you want to filter by
function checkCondition(){
//var fieldName = 'your_field_name'; // Replace with your field name
//ar fieldValue = 'desired_value'; // Replace with the value you want to filter by

// Create a GlideRecord object for the User table
var gr = new GlideRecord('sys_user');

// Add a query to filter users based on the specific field value
gr.addQuery('u_hrdepartment', 'CONTAINS',  'Information Technology');

// Execute the query
gr.query();

}
 
Attempt 2
answer = evaluateUser(user_id)

function evaluateUser(user_id) {

    var loggedInUser = new GlideRecord('sys_user');
    answer = false;
    loggedInUser.addEncodedQuery('active=true^u_hrdepartmentLIKEinformation technology^user=' + user_id)
    loggedInUser.query()
    if (loggedInUser.next()) {
        answer = true;
    }
    return answer;
}
 
Attempt 3
 
    var loggedInUser = new GlideRecord('sys_user');
    loggedInUser.get(gs.getUserID());
    if (loggedInUser.u_hrdepartment.indexOf("Information Technology") !== -1) {
        answer = true;
    } else {
        answer = false;
    }
 
Attempt 4
 
y
function evaluateUser() {

    var loggedInUser = new GlideRecord('sys_user');
    loggedInUser.get(gs.getUserID());
    var department = loggedInUser.u_hrdepartment;
    if (department.includes("Information Technology")) {
        return true;
    } else {
        return false;
    }
 
I've tried a few other variations of the above scripts but none of them to seem to work, what am i doing wrong here?
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

A couple of things to clarify - Are you trying to affect Service Catalog Items or Knowledge articles?  Are you adding the User Criteria to the Available For or Not Available For related list - if the current user is a member of IT should this item/article be seen or not seen?  After adding User Criteria, make sure you are ending impersonation or logging out as the test user before retesting.

 

1 is rubbish as it doesn't incorporate anything about the current user, and doesn't return true or false.

3 and 4 use gs.getUserID() which the comment in the Script field on new criteria explicitly mentions NOT using, so let's go with 2 as a good starting point.  The encoded query is incorrect as there is not a field on the sys_user table named 'user' so that entire query is being ignored.  Try this in its place:

loggedInUser.addEncodedQuery('active=true^u_hrdepartmentLIKEinformation technology^sys_id=' + user_id);

as user_id holds the sys_id of the user being evaluated, and sys_id is the name of the field on the user table.

 

Service Portal has the extra layer of sometimes needing to clear cache (cache.do in the URL) before changes are seen, so try testing in the native UI as well.  You can also run User Criteria Diagnostics in the left nav to determine if a user should or should not see a certain item or article.

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

A couple of things to clarify - Are you trying to affect Service Catalog Items or Knowledge articles?  Are you adding the User Criteria to the Available For or Not Available For related list - if the current user is a member of IT should this item/article be seen or not seen?  After adding User Criteria, make sure you are ending impersonation or logging out as the test user before retesting.

 

1 is rubbish as it doesn't incorporate anything about the current user, and doesn't return true or false.

3 and 4 use gs.getUserID() which the comment in the Script field on new criteria explicitly mentions NOT using, so let's go with 2 as a good starting point.  The encoded query is incorrect as there is not a field on the sys_user table named 'user' so that entire query is being ignored.  Try this in its place:

loggedInUser.addEncodedQuery('active=true^u_hrdepartmentLIKEinformation technology^sys_id=' + user_id);

as user_id holds the sys_id of the user being evaluated, and sys_id is the name of the field on the user table.

 

Service Portal has the extra layer of sometimes needing to clear cache (cache.do in the URL) before changes are seen, so try testing in the native UI as well.  You can also run User Criteria Diagnostics in the left nav to determine if a user should or should not see a certain item or article.

 

thank you! i'm mad at myself for not catching that.  I was getting far to deep and ignoring simple things!

Sounds like some things should have been clearer or corrected on the 'solved' posts too.  Glad you got it working!