UserCriteriaLoader API and Advanced Script User Criteria

brianhaak
Kilo Contributor

I'm trying to utilize the User Criteria Loader API to identify the User Criteria associated with a particular user.

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=UCLS-getAllUserCriteria

I am able to use the function "getAllUserCriteria(String userId)" and provide the sys_id of the user I am interested in. This returns an array of sys_ids of the user criteria records that match. The problem I am having is that for all our User Criteria which use scripts (advanced = true), when the UserCritieria Loader function is called, the script within the user criteria always runs based on the logged in user, not the one provided in the function parameter. I figure this is because in our user criteria script, we use `var userid = gs.getUserID();` and then determine based on that user if the result is true or false.

Does anyone know how to use the UserCriteriaLoader API with User Criteria that uses advanced scripts? Is there a way from within the User Criteria script, to identify the user being looked at without using gs.getUserID()?

Here is a simplified example. Given the below User Criteria Script, if I call getAllUserCriteria('87f606c7db11230049027dda8c9619de') (where I provide a user's sys_id), I would expect the returning array to include this User Criteria. However, when I test this in my personal dev (OOB London), this user criteria is only included in the resulting array if I am logged in as that particular user.

var userid  = gs.getUserID();

if (userid == '87f606c7db11230049027dda8c9619de')
   answer = true;
else
   answer = false;
1 ACCEPTED SOLUTION

Jakub Pobiecky
Tera Expert

In user criteria script, you can user variable user_id instead of gs.getUserID(). It is probably passed from some upper function.
I tried it on Madrid version and it works. It holds user id, which is provided via UserCriteriaLoader.

I has less impact than storing it in user session, but as it is not officially documented, I am not sure, how reliable this will be in the future.

View solution in original post

7 REPLIES 7

Eric TRAVELLA
Tera Contributor

Hi,

I recently found this API, that is useful.

Indeed, it's a problem with advanced scripted User Criterias.

I have not yet set up this workaround, but I think solve the problem by creating a temporary session variable and use it in User Criterias with advanced scripts if it's not null. If it's null, I will take the gs.getUserID().

 

--> Before calling the API, I think initialize the variable like this :

gs.getSession().putClientData('specificUser', sys_id_of_the_user);
var result = new sn_uc.UserCriteriaLoader.getAllUserCriteria(sys_id_of_the_user);

gs.info(result);
gs.getSession().putClientData('specificUser', null);

--> Within all advanced scripted User Criterias :

if(gs.getSession().getClientData('specificUser') == null)

THEN use gs.getUserID

ELSE use gs.getSession().getClientData('specificUser')

 

 

The disadvantage is to test the presence of the value in each script, but I think it's useful. 😉

brianhaak
Kilo Contributor

Interesting idea Eric, thanks for your reply! I ended up going a different route, but hopefully your response will be helpful to others.

I was working on a scoped application and needed a solution quickly. I ended up creating my own custom method for handling user criteria within the scoped app. It is less flexible than the advanced scripts of the OOB User Criteria, but gets the job done. And we needed to avoid both modifying all our existing User Criteria scripts, and getting potentially unreliable results.

I created a new table for holding the custom user criteria. The fields on the table include lists of fields from the user record (Department, Location, and Country) as well as Name, Active, Order and Match-All.

 

find_real_file.png

 

Here is the Script include I used to get the custom user criteria associated with a given user. The queryString does some And and Or logic depending on if "Match-All" is checked or not. It will do OR logic with one field (for the screenshot above a user could be in Austria or Belgium), and it will do OR logic between fields if Match All is unchecked. If it is checked, then it does AND logic between fields.

Note that we have a custom table called Country ISO Code which is referenced on the location table.

I was also only interested in the first one that matched, sorted by order.

getUserCriteriaMapping: function(user_id) {
		
	var grUser = new GlideRecord('sys_user');
	if (grUser.get('sys_id', user_id))
		{
		var queryString = "active=true^match_all=true^country_iso_codeLIKE"+ grUser.location.u_iso_code 
+"^ORcountry_iso_codeISEMPTY^departmentLIKE"+ grUser.department 
+"^ORdepartmentISEMPTY^locationLIKE"+ grUser.location 
+"^ORlocationISEMPTY^NQactive=true^match_all=false^country_iso_codeLIKE"
+ grUser.location.u_iso_code +"^ORdepartmentLIKE"+ grUser.department 
+"^ORlocationLIKE"+ grUser.location;
			
		var grMappings = new GlideRecord("custom_user_criteria"); //custom table name here
		grMappings.orderBy("order");
		grMappings.addEncodedQuery(queryString);
		grMappings.query();
		if (grMappings.next()) {
			return grMappings;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}

Jakub Pobiecky
Tera Expert

In user criteria script, you can user variable user_id instead of gs.getUserID(). It is probably passed from some upper function.
I tried it on Madrid version and it works. It holds user id, which is provided via UserCriteriaLoader.

I has less impact than storing it in user session, but as it is not officially documented, I am not sure, how reliable this will be in the future.

Hi,

I think user_id variable in user criteria script seems to have some issue in NewYork. Did you come across such issue?

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader