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

Ankur Bawiskar
Tera Patron
Tera Patron

@Jeck Manalo 

if you want to filter then you should use advanced reference qualifier.

If you want to set value then you should use onChange client script

what type of variable is that?

do you want to show all the locations in the hierarchy upto the top based on requested for? 

if yes then use this in reference qualifier, check below link and enhance

Recursive function returns undefined 

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

all i want is to filter so i have tried this , this work but it will filter all user that has same location with the current user. my goal is to filter it to the Region which is Location.Parent.Parent

 

JeckManalo_0-1748836248144.png

 

@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