How to populate list collector based on reference field value

neharao
Giga Contributor

Hi All,

I have a requirement where I have a "country" field reference to "core_country" and when a country is selected here I have to populate the list collector having "catalog item" to be categorised based on "country" field selected. i.e I need to fetch the cat items based on "Country" .If the country is not found in the list collector then the list collector should not appear or may be some message to be displayed .

Thanks,

Neha

9 REPLIES 9

AnirudhKumar
Mega Sage
Mega Sage

An array of sys_ids is all that is required to set values to a list collector.

 

Long Live ServiceNow!

The SN Nerd
Giga Sage
Giga Sage

What have you tried so far? I am not sure that I understand the requirement.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,

Please find the screenshot below. Here there is a field called user country. So when I fill in the country, it should populate the list collector with the cat items having the country as mentioned in the "user country". For example if i choose France in the "User Country" then it should pick the catalog items which has "France" as their country.

find_real_file.png

Write an onChange Client Script on the User Country field:

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

var ga = new GlideAjax('GetCatalogsByCountry');
ga.addParam('sysparm_name','catalogsByCountry');
ga.addParam('sysparm_country',newValue);
ga.getXML(getResponse);
	
	
}


function getResponse(response) {
var catalogItems = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('ListCollectorName' , catalogItems);//Set your field name
   
}

 

Write a script include as shown below:

var GetCatalogsByCountry = Class.create();
GetCatalogsByCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
catalogsByCountry: function() {
	   
var itemsList = '';
	   
var item = new GlideRecord('sc_cat_item');
item.addQuery('u_country', this.getParameter('sysparm_country'));
item.query();

while(item.next())
{
itemsList = itemsList + item.sys_id.toString() + ',';
}

return itemsList;

} 
});

 

Note : In the script include, I've used u_country as the field on sc_cat_item. Check if that is the name of your field, if not pls replace.

Let me know if it works.

 

Long Live ServiceNow!