Filter the reference sys_user field based on script include & on-change script

Jeck Manalo
Tera Guru

 

Is this possible to filter the field of reference sys_user into same Region of Requested For ?

 

Unfortunately this ain't work.

 

Sample Script:

 

Script Include:

var UserLocationFilter = Class.create();
UserLocationFilter.prototype = {
    initialize: function() {
    },

    getUsersByLocation: function(userId) {
        var userRecord = new GlideRecord('sys_user');
        if (userRecord.get(userId)) {
            var userLocation = userRecord.getValue('location');
            var parentLocation = new GlideRecord('cmn_location');
            if (parentLocation.get(userLocation)) {
                var grandParentLocation = parentLocation.getValue('parent');
                if (grandParentLocation) {
                    var grandParent = new GlideRecord('cmn_location');
                    if (grandParent.get(grandParentLocation)) {
                        var grandParentParentLocation = grandParent.getValue('parent');
                        if (grandParentParentLocation) {
                            var users = new GlideRecord('sys_user');
                            users.addQuery('location.parent.parent', grandParentParentLocation);
                            users.query();
                            var userList = [];
                            while (users.next()) {
                                userList.push(users.sys_id.toString());
                            }
                            return userList.join(',');
                        }
                    }
                }
            }
        }
        return '';
    },

    type: 'UserLocationFilter'
};
 
 
On-Change

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var requestedFor = newValue;
    var ga = new GlideAjax('UserLocationFilter');
    ga.addParam('sysparm_name', 'getUsersByLocation');
    ga.addParam('sysparm_user_id', requestedFor);
    ga.getXMLAnswer(function(response) {
        var userList = response.split(',');
        var filterCondition = 'sys_idIN' + userList.join(',');
        g_form.setValue('variable_name', filterCondition);
    });
1 ACCEPTED SOLUTION

@Jeck Manalo 

The link I shared shows how to perform the recursion to get all the values in that hierarchy, simply enhance it for location.

Thank you for marking my response as helpful.

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

View solution in original post

7 REPLIES 7

YaswanthKurre
Giga Guru

Hi Jeck,

 

 If I understood your request properly, you don't necessarily need to write a script.

 

You can use reference qualifiers to achieve this.

 

Please refer these links:

1. Product documentation: https://www.servicenow.com/docs/bundle/yokohama-platform-administration/page/script/server-scripting...

2. Community link: https://www.servicenow.com/community/developer-articles/reference-qualifiers-in-servicenow/ta-p/2765...

 

Please mark this as helpful and correct if this helps you,

 

Thanks,

Yaswanth

 

 

 

I have tried it already by just doing this simple script , unfortunately it will only get the location not the Region which i tried.

 

Location>Parent>Parent then script is javascript:gs.getUser().getLocation().getParent().getParent()

 javascript:gs.getUser().getLocation()

 

this one it will only populate all record that has same location with logged on user. but in my case is i need to dotwalk into location.parent.parent so i will get the Region.

JeckManalo_0-1748835842219.png

 

Hi Jeck,

 

So here in Location filter, instead of using gs.getUser and location use a script include and define your condition inside script include to only get those values.

 

Go with advanced reference qualifier and pass the current.Location>Parent>Parent to script include and use it to filter to only get those values.

 

This approach will work for you.

 

Thanks,

Yaswanth.