Record Producer - If Country = .... Cities are... (UI Policy)

AngelP83
Giga Guru

Hello,

I need a suggestion of how to do the following:

I imported a database of Countries and Cities.  I need to create a Catalog Item (record producer... already created). 

I have 2 tables where the data is stored (Country_Cities)

The table is something like this

CountryCity
USAArizona
USAFlorida
USATexas
ItalyRome
MexicoGuadalajara
MexicoChiapas

          

Ok.. so there are over 2000 records.  

The record producer at the moment has 2 variables.. Countries and Cities

The cities will depend on the country they have selected previously.

The country variable is a LOOKUP SELECT BOX type.  Using Unique Values only, so it can remove the duplicates.

Now... Should I use a UI Policyto look at the Country field, and find all the Cities for that particular field? and if so.. how can I do that? or another easier way?

 

At the end, this is what I would need.

If they Select USA in the country.. from the record producer..

The cities will be display (only USA cities).

 

Thank you all

1 ACCEPTED SOLUTION

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @AngelP83 ,

Write the onchange client script on the countries field and pass the country name to Script include gliderecord the table and return the cities in cities field.

Please modify the below code

Client script:

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

	var country = g_form.getValue('country');

	//Call script include
	var ga = new GlideAjax('global.sampleUtils');   //Scriptincludename
	ga.addParam('sysparm_name', 'getcitiesDetails'); //Method
	ga.addParam('userId',country ); //Parameters
	ga.getXMLAnswer(getResponse);
	
	function getResponse(response){
		console.log(response);
		var res = JSON.parse(response);
		console.log(res);
		g_form.setValue('cityfieldname',res.cities);
	}
	
}

 

Script include:

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getcityDetails: function(){
		gs.addInfoMessage('script include triggered');
		
		var userId = this.getParameter('userId');
			
		obj = {};

	var grSysUser = new GlideRecord('yourtable name');
       grSysUser.addQuery('country',userId );
	while(grSysUser.next)) {
		
		obj.cities=  grSysUser.getValue('city fieldname');
	}
	gs.addInfoMessage(obj+JSON.stringify(obj));
	return JSON.stringify(obj);
	},

	type: 'sampleUtils'
});

 

OR

You can use Advance reference qualifier in the Cities and call this scritp inlcude direclty 

 javascript : new scriptinclduename.functionname(current.variables.countytry);

 

Thank you,

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

View solution in original post

1 REPLY 1

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @AngelP83 ,

Write the onchange client script on the countries field and pass the country name to Script include gliderecord the table and return the cities in cities field.

Please modify the below code

Client script:

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

	var country = g_form.getValue('country');

	//Call script include
	var ga = new GlideAjax('global.sampleUtils');   //Scriptincludename
	ga.addParam('sysparm_name', 'getcitiesDetails'); //Method
	ga.addParam('userId',country ); //Parameters
	ga.getXMLAnswer(getResponse);
	
	function getResponse(response){
		console.log(response);
		var res = JSON.parse(response);
		console.log(res);
		g_form.setValue('cityfieldname',res.cities);
	}
	
}

 

Script include:

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getcityDetails: function(){
		gs.addInfoMessage('script include triggered');
		
		var userId = this.getParameter('userId');
			
		obj = {};

	var grSysUser = new GlideRecord('yourtable name');
       grSysUser.addQuery('country',userId );
	while(grSysUser.next)) {
		
		obj.cities=  grSysUser.getValue('city fieldname');
	}
	gs.addInfoMessage(obj+JSON.stringify(obj));
	return JSON.stringify(obj);
	},

	type: 'sampleUtils'
});

 

OR

You can use Advance reference qualifier in the Cities and call this scritp inlcude direclty 

 javascript : new scriptinclduename.functionname(current.variables.countytry);

 

Thank you,

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.