User Criteria Advanced Script to include users for child locations

SarahM584850592
Tera Contributor

Hello,

 

I have a user criteria set with location as 'north america'. Canada and mexico are locations with parent as north america. But the users from canada and mexico are not getting included in the criteria. I need a script to include them. Can you assist me with this please.

 

Thanks,

1 ACCEPTED SOLUTION

M Iftikhar
Mega Sage

Hi @SarahM584850592 ,

By default, user criteria doesn’t automatically include child locations when you set a parent location. To include Canada and Mexico, you’ll need to extend the user criteria with a script.

Example approach:

  1. Create a Scripted User Criteria.

  2. Add logic to check the user’s location and also the parent location.

Sample script:

 


(function() {
var user = gs.getUser();
var location = new GlideRecord('cmn_location');
if (location.get(user.getLocation())) {
// Direct match
if (location.name == 'North America')
return true;

// Check parent location
if (location.parent && location.parent.name == 'North America')
return true;
}
return false;
})();


This way, users in "North America" and its child locations (Canada, Mexico) will meet the criteria.

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@SarahM584850592 

that's OOTB behavior.

You will have to get logged in user country and compare it with those 3

You can use Advanced Script like this

Note: It's recommended to use user_id instead of gs.getUserID() in user criteria script

user_id variable usage for user criteria 

		var gr = new GlideRecord("sys_user");
		gr.addQuery("sys_id", user_id);
		gr.addQuery("location.name", "IN", "Canada,Mexico,North America"); // give correct location name here
		answer = gr.hasNext();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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