Alikutty A
Tera Sage

User criteria is a powerful feature that allows you to setup data driven access controls by evaluating the user attributes of a logged-in user. It is extensible enough by allowing you to configure additional user attributes and also provide an advanced script evaluator that could add logic beyond the configurations. They are primarily used for controlling user access on service portals, catalog items, knowledge articles, HR services, HR contents etc. With a proper API, they could be extended further or used in any of your custom implementations.

ServiceNow has an undocumented API named UserCriteriaLoader which is used at multiple scripts within the instance. It can be used to control access and dynamically evaluate a user criteria for your custom solutions. Below, I am explaining all the methods that I was able to discover while using the same for one of my implementation.

 
1. getAllUserCriteria - Returns an array of all user criteria's matched by the logged in user
var allCriterias = new sn_uc.UserCriteriaLoader.getAllUserCriteria(gs.getUserID());
gs.info(allCriterias);
gs.info(allCriterias.length);
 
2. userMatches - Validates whether the user matches any of the user criteria's listed in the array and returns a boolean value ie true or false 
var userCriterias = ['6b6df061ff2121009b20ffffffffff44','0b5b0e6453631300afffddeeff7b1201'];
var userMatches = sn_uc.UserCriteriaLoader.userMatches(gs.getUserID(), userCriterias);
gs.info(userMatches);
 
3. getMatchingCriteria - Checks if the user criteria's passed in an array is matched by the user ID and returns the matching ones
var userCriterias = ['6b6df061ff2121009b20ffffffffff44','0b5b0e6453631300afffddeeff7b1201'];
var matchingCriterias = sn_uc.UserCriteriaLoader.getMatchingCriteria(gs.getUserID(), userCriterias);
gs.info(matchingCriterias);
 
4. getUserCriteria - Returns the list of user criteria's that are matched by the user from a specific table. For eg sc_cat_item_user_criteria_mtom or Catalog Item Available For is the table used to control access for catalog items and the function will return all user criteria's from this table that are matched by the user
var getCriterias = new sn_uc.UserCriteriaLoader.getUserCriteria(gs.getUserID(),'sc_cat_item_user_criteria_mtom');
gs.info(getCriterias);

 

SNC.UserCriteriaLoader is another related API used in the instance but it is not supported in scoped applications.

 

Note: The user criterias are by default not overridden by admin users and you will have to explicitly grant them access. Script based user criterias are cached, hence the changes wont reflect unless the user has logged out of the instance.

 

 

10 Comments