Dynamic drop down for catalog variable.

venkateshdhanap
Tera Contributor

I have two variables Country and state(variable name also same country and state), Both are drop down variables.Country has 3 options Australia, India, China. I need to provide state dropdown option based on the country selected

if country = Australia state options should be Australian Capital Territory, New South Wales, Northern Territory, South Australia, Queensland, Tasmania, Victoria, Western Australia;

if country is not   Australia state option should be only :Not Applicable. I thing for this catalog client script required, Please provide script for this.

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

You can use like this



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }


  g_form.clearOptions('state');


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


  alert(val);


  if(val=='Australia')


  {


  alert('inside');


  g_form.addOption('state', '1', 'statename');


  g_form.addOption('state', '2', 'statename');


  }


  else if(val=='India')


  {


  alert('inside');


  g_form.addOption('state', '1', 'statename');


  g_form.addOption('state', '2', 'statename');


  g_form.addOption('state', '3', 'statename');


  }



}


Regards
Harish

View solution in original post

9 REPLIES 9

Hi sirsk,

 

For your solution, you need to define the Catalog Client Script and Script Include as below. Refer the below code snippet and tweak as per your requirement.

Catalog Client Script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Clear the options first
	g_form.clearOptions('category'); //Pass the proper variable name
	g_form.clearOptions('subcategory'); //Pass the proper variable name
	
	var ga = new GlideAjax('servicecategorieslookup');
	ga.addParam('sysparm_name', 'getServiceRelatedValues');
	ga.addParam('sysparm_ser', newValue);
	ga.getXML(retrieveValue);
}

function retrieveValue(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	answer = answer.evalJSON();
	
	g_form.addOption('category', answer.cat, answer.cat); //Pass the proper variable name
	g_form.addOption('subcategory', answer.subcat, answer.subcat); //Pass the proper variable name
}

 

Script Include

var servicecategorieslookup = Class.create();
servicecategorieslookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getServiceRelatedValues: function(){
		var service = this.getParameter('sysparm_ser'); //Retrieve the value from Catalog Client Script
		var gr = new GlideRecord('u_service_category_lookup'); //Pass the table name
		if(gr.get(service)) {
			var result = {
				cat: gr.getValue('u_category'), //Pass the Category field name
				subcat: gr.getValue('u_subcategory')
			};
			
			var json = new JSON();
			result = json.encode(result);
			return result;
		}
	},
	
	type: 'servicecategorieslookup'
});

 

I hope this helps.Please mark correct/helpful based on impact

Harish KM
Kilo Patron
Kilo Patron

g_form.addOption('state', '1', 'none');


Regards
Harish

Yes i tried this. If i add like this form accepting none also as a state


To get None to appear in your list but not to be considered a value for state use the following:



g_form.addOption('state', '', '-- None --');



Please mark as correct/helpful if you find it as such.


Harish KM
Kilo Patron
Kilo Patron

what exactly your lookin for?


None also will be a drop down value.


Regards
Harish