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

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.

Nilesh Pol
Tera Guru

@SarahM584850592 The “Location” field in User Criteria doesn’t automatically include child locations — you need to handle that in a script.

 

(function() {
var parentLocation = 'North America'; //  parent location

var parentLoc = new GlideRecord('cmn_location');
parentLoc.addQuery('name', parentLocation);
parentLoc.query();
if (!parentLoc.next()) {
gs.warn('User Criteria Script: Parent location not found: ' + parentLocation);
return false;
}

// --- Build list of all child locations (including the parent) ---
var allLocIds = [parentLoc.sys_id.toString()];

var childLoc = new GlideRecord('cmn_location');
childLoc.addQuery('parent', parentLoc.sys_id);
childLoc.query();
while (childLoc.next()) {
allLocIds.push(childLoc.sys_id.toString());
}

// --- Check if the current user’s location is in that list ---
if (gs.getUser().getLocationID() && allLocIds.indexOf(gs.getUser().getLocationID()) !== -1) {
return true;
}
return false;
})();

You can paste into the Script field of your User Criteria

Rafael Batistot
Tera Sage

Hi @SarahM584850592 

 

Here the script include 

 

// Get current user's location sys_id
var userGR = new GlideRecord('sys_user');
if (userGR.get(gs.getUserID())) {
var userLocation = userGR.getValue('location');

if (userLocation) {
// Get sys_id of "North America"
var northAmericaGR = new GlideRecord('cmn_location');
northAmericaGR.addQuery('name', 'North America');
northAmericaGR.query();

if (northAmericaGR.next()) {
var northAmericaSysId = northAmericaGR.getValue('sys_id');

// Direct match
if (userLocation == northAmericaSysId) {
answer = true;
} else {
// Walk up the hierarchy
var currentLocationGR = new GlideRecord('cmn_location');
if (currentLocationGR.get(userLocation)) {
var parent = currentLocationGR.getValue('parent');
while (parent) {
if (parent == northAmericaSysId) {
answer = true;
break;
}
var parentGR = new GlideRecord('cmn_location');
if (!parentGR.get(parent)) {
break;
}
parent = parentGR.getValue('parent');
}
}
}
}
}
}

if (typeof answer == 'undefined') {
answer = false;
}
return answer;

SunilKumar_P
Giga Sage

@SarahM584850592 can you try the below script?

var naLocationId;
var locationGr = new GlideRecord('cmn_location');
locationGr.addQuery('name', 'North America');
locationGr.query();
if (locationGr.next()) {
    naLocationId = locationGr.sys_id;
}

var user_id = gs.getUserID();
var userLocation;
var userGR = new GlideRecord('sys_user');
if (userGR.get(user_id)) {
    userLocation = userGR.location.parent;
}

var isNaUser = false;
var locationGr2 = new GlideRecord('cmn_location');
if (locationGr2.get(userLocation)) {
    var parentId = locationGr2.parent;

    if (parentId == naLocationId) {
        isNaUser = true;

    }
}

answer = isNaUser;