How to filter referenced field values

Sourabh22
Tera Contributor

I have a catalog form which contains location variable referenced to location table.
I want to show only unused values of location table in form.
I don't want to show the values used by user table.

How to achieve this????

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Sourabh22 

 

You can write an Script Include , assume 'ExcludeLocation' and a function called 'getExcludedLocations()' and you can write below code in it:

 

 

var usrLoc= [];
var usr = new GlideAggregate('sys_user');
usr.addAggregate('COUNT', 'location');
usr.addEncodedQuery("location!=NULL");
usr.groupBy('location');
usr.query();

while(usr.next())
{
usrLoc.push(usr.getValue('location'));
}

var locArr = [];
var loc = new GlideRecord('cmn_location');
loc.addEncodedQuery("sys_idNOT IN"+locArr);
loc.query();

while(loc.next())
{
locArr.push(loc.getUniqueValue());
}

return "sys_idIN"+locArr;

 

 

You can now call this Script Include in your Reference Qualifier of Variable using below link:

 

 

javascript: new ExcludeLocation().getExcludedLocations();

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

20 REPLIES 20

Hi @Sourabh22 

 

Sorry, small mistake and correction in the code above:

 

loc.addEncodedQuery('sys_idNOT IN'+usrLoc);

 

Replace locArr with usrLoc

 

Try now and see.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi @AnubhavRitolia ,
I have already replaced locArr with usrLoc in that line and I have checked in background script there return giving acccurate list. But it is not reflecting in the form. In form it is showing all values of the location field.
In my form the field name is location referenced to cmn_location table 
I used reference qualifier  Advanced 
javascript: new ExcludeLocation().getExcludedLocations();

@Sourabh22 

 

Could you share your Script Include full code to understand it better?

Hope it is not client callable.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

var ExcludeLocation = Class.create();
ExcludeLocation.prototype = {
    initialize: function() {
    },
    getExcludedLocations: function(){
        var usrLoc= [];
        var usr = new GlideRecord('sys_user');
        usr.addEncodedQuery('locationISNOTEMPTY');
        usr.query();

        while(usr.next())
        {
        usrLoc.push(usr.getValue('location'));
        }

var locArr = [];
var loc = new GlideRecord('cmn_location');
loc.addEncodedQuery("sys_idNOT IN"+usrLoc);
loc.query();

while(loc.next())
{
locArr.push(loc.getUniqueValue());
}

return locArr;
    },

    type: 'ExcludeLocation'
};
it is not client callable

@Sourabh22 

 

This is not the code I have given. My code is having GlideAggregate. Could you please try that one and see.

 

Also your return is different than I mentioned. 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023