Auto Populate or filter a reference field based on the choice in 'select box' type variable.

iDevilsdocry
Mega Contributor

Hello,

I have a catalog item with a choice field. Further down the form there is another reference field that references to groups. I need to customize it so that it will either auto populate based on that the value for the first choice field. 

OR

It can show filtered results of the corresponding groups. (This is preferred since some choices may relate to multiple groups that I can pick one from. 

I would like to know if this is possible to do this just with a client script or do I need script include aswell. 

1 ACCEPTED SOLUTION

Xavier Cordero
Kilo Sage

Hello iDevilsdocry,

Both options are possible (both as separate, or both could be working at the same time, where the reference field would only automatically populate if there is only one option to select). I'll share with you an example I made for this to filter the groups based on the selection within the select box. I made this example for the Incident table, where the select box is the state field and the reference field is just referencing the incidents.

You'll need to create a Script include to create what is called an advanced Reference Qualifier.  In my case I have named my script include "CheckSelectBox", the code is below:

var CheckSelectBox = Class.create();
CheckSelectBox.prototype = {
    initialize: function() {
    },
	Check: function(){
		var state = current.variables.select_box; //Change select_box for the name of your select_box variable.
		var inc = new GlideRecord('incident');//Change this for the table the reference field is referencing.
		inc.addQuery('state',state);//Change this for the specific query on the field that you are making.
		inc.query();
		var finalList = [];
		while(inc.next()){
			finalList.push(inc.getValue('sys_id'));
		}
		return 'sys_idIN' + finalList.join(',');
		
	},
    type: 'CheckSelectBox'
};

After you create your script include, go into the reference field variable and change the reference qualifier field to advanced. From there, simply call the script include and your values should filter to only show the relevant values based on what is selected on the select_box.

find_real_file.png

The other example, which is the population of the reference field based on the selection within the select box can be completed through a GlideAjax Call. You'll need to create a Client Callable Script include that will be called from an onChange Catalog Client Script that will trigger on the change of the select box field. Here is a link that shows some examples of GlideAjax calls within the system: https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference... 

View solution in original post

5 REPLIES 5

Xavier Cordero
Kilo Sage

Hello iDevilsdocry,

Both options are possible (both as separate, or both could be working at the same time, where the reference field would only automatically populate if there is only one option to select). I'll share with you an example I made for this to filter the groups based on the selection within the select box. I made this example for the Incident table, where the select box is the state field and the reference field is just referencing the incidents.

You'll need to create a Script include to create what is called an advanced Reference Qualifier.  In my case I have named my script include "CheckSelectBox", the code is below:

var CheckSelectBox = Class.create();
CheckSelectBox.prototype = {
    initialize: function() {
    },
	Check: function(){
		var state = current.variables.select_box; //Change select_box for the name of your select_box variable.
		var inc = new GlideRecord('incident');//Change this for the table the reference field is referencing.
		inc.addQuery('state',state);//Change this for the specific query on the field that you are making.
		inc.query();
		var finalList = [];
		while(inc.next()){
			finalList.push(inc.getValue('sys_id'));
		}
		return 'sys_idIN' + finalList.join(',');
		
	},
    type: 'CheckSelectBox'
};

After you create your script include, go into the reference field variable and change the reference qualifier field to advanced. From there, simply call the script include and your values should filter to only show the relevant values based on what is selected on the select_box.

find_real_file.png

The other example, which is the population of the reference field based on the selection within the select box can be completed through a GlideAjax Call. You'll need to create a Client Callable Script include that will be called from an onChange Catalog Client Script that will trigger on the change of the select box field. Here is a link that shows some examples of GlideAjax calls within the system: https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference... 

Unfortunately, my requirement was changed and no longer relevant to your solution. However, I did try your first example on my personal instance for a different field on a different table and it worked like a charm.

Thanks for your time and solution.

Hi Xavier,

    This seems pretty straight forward and I changed all the parameters but it didn't seem to work for me. Not sure if you are available to provide any assistance.

So my table name = Floor (fpv_floorplan). The two fields in this table (Name = floor and Building = the building associated with the floor).

My catalog item has a field that is a select box named building_location

What I am trying to do is if I select a specific location (from the select box) = 1825 Rodeo Drive, then from the table I want to only show the floors available for building = 1825 Rodeo Drive

Here is the script that I used but it doesn't seem to be working. I also put the reference qualifier into my variable in the catalog item.

 

var CheckSelectBox = Class.create();
CheckSelectBox.prototype = {
initialize: function() {
},
Check: function(){
var building = current.variables.building_location; //Change select_box for the name of your select_box variable.
var floor = new GlideRecord('fpv_floorplan');//Change this for the table the reference field is referencing.
floor.addQuery('building',building);//Change this for the specific query on the field that you are making.
floor.query();
var finalList = [];
while(floor.next()){
finalList.push(floor.getValue('sys_id'));
}
return 'sys_idIN' + finalList.join(',');

},
type: 'CheckSelectBox'
};

find_real_file.png

find_real_file.png

Very Helpful post!! Worked like charm..