Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Advanced User criteria script not working for knowledge bases

laura_p
Giga Guru

I have a requirement to only allow people to view into a knowledge base that have a specific role, in a specific group, in a certain company and have a specific service level. The role, group, and company are fields on the user criteria form that I have added. However the service level field is a custom field on the user record. So I have selected Advanced and added this portion as a script. Match All has been selected.

 

However, people that are not the service level are still showing up as allowed to view the knowledge base. It blocks people that are not in the role, group and company but if they are of a different service level they can still read. I have tried different types of scripts including one below and another written as a function instead. I have tried using both the display name (blocks everyone even in IT) or the sysID (allow everyone including those not in IT). They all work in background script when testing and show the correct true/false value, but it seems like it doesn't care if it true or false. I have verified the users testing with that they do not have access through an ACL or other user criteria.

 

I make sure to log off and log back in after updating the user criteria record. I also give it some time before testing but not working. Anyone seen this before?

 

var isIT = (gs.getUser().getRecord().getValue('u_person_sla') == 'xxxxxxxxxxxxx627beae3ee41f');

if (isIT) {
    // User is IT
    answer = true;
} else {
    // User is not in IT
    answer = false;
}
 
Thank you.
1 REPLY 1

jcmings
ServiceNow Employee

There is an explicit recommendation not to use gs.getUser(). This appears by default when you open the Script editor:

/** 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.
*/

 

Based on my testing a few years ago, I believe the script check overrides the other checks. It's been a bit since I last tinkered with user criteria, but I would recommend adjusting your script based on the above guidance first, and then potentially doing all of your checks via script.