How to set the Values in a drop down based on the select value in 1st drop down using catalog client script

Gowtham Kodali
Tera Contributor

Hi,

 

I have 2 drop downs in a catalog item I added all the possible values  to the drop down ,

Now if I select option A in the First drop down I have to get the options   X,Y,Z in the second drop down

if I select option B in first dropdown I have to get options  R,S,T

 

I have used a catalog client script and 

 

i am trying like this 

 

var val = g_form.getValue('DropDown1 fieldName');

if(val == 'dropdown option value')

{

g_form.setValue('dropdown2fieldname','dropdown2optionvalue')

}

any wrong in that

how can i achieve this 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi

There are 2 ways.

1. you can configure option2 to be dependent on option 1. In this case the values of dropdowns should come from database.

2. If the values of dropdowns are static, then write onChange client script on option 1 and then add code like below

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if(newValue == "A") {
       g_form.clearValues("option2");//put actual field name
       g_form.addOption("option2","X","X");
       g_form.addOption("option2","Y","Y");
       g_form.addOption("option2","Z","Z");
   }
}

Mark the comment as a correct answer and helpful if this helps.

View solution in original post

18 REPLIES 18

Allen Andreas
Administrator
Administrator

Hi,

You'd want to make sure you're using an onChange client script and select that first drop-down as the field.

Then in the script section you'd do something like:

var val = g_form.getValue('DropDown1 fieldName');
if(val == 'dropdown option value'){
g_form.removeOption('dropdown2fieldname','dropdown2option1value');
g_form.removeOption('dropdown2fieldname','dropdown2option2value');
g_form.removeOption('dropdown2fieldname','dropdown2option3value');
} else if (val == 'dropdown option value 2') {
g_form.removeOption('dropdown2fieldname','dropdown2option4value');
g_form.removeOption('dropdown2fieldname','dropdown2option5value');
g_form.removeOption('dropdown2fieldname','dropdown2option6value');
}

So this removes options from the select-box so that only the other choices remain.

The title of your post is a bit different from the content of your question, but this is how you'd remove unwanted choices if another field was a certain selection.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sagar Pagar
Tera Patron

Hello Gowtham,

Please try this script.

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

			g_form.clearValue('dropdown2fieldname');
			return;
		}

		if(newValue == 'dropdown option value')
		{
			g_form.addOption('dropdown2fieldname', 'dropdown2optionvalue', 'dropdown2optionlabel');
		}
	}

 

 

Thanks,

Sagar Pagar

The world works with ServiceNow

I tried this 

 

i kept an alert  inside the IF as well but its not going into the IF condition

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

			g_form.clearValue('dropdown2fieldname');
			return;
		}

		if(newValue == 'dropdown option value')
		{
			g_form.addOption('dropdown2fieldname', 'dropdown2optionvalue', 'dropdown2optionlabel');
		}
	}

 

Have you select the dropdown1fieldname variable in variable field of catalog client script.

find_real_file.png

The world works with ServiceNow

Hello Gowtham,

in the script, you need to compare the value (NOT Label). check if the value is having spaces?