removeOption and addOption

ceraulo
Mega Guru

Hello!

I have 2 variables X & Y.

Variable X is a Select Box with values A & B
Variable Y is a Select Box with values 1, 2, 3, 4, 5, 6

I want to achieve are:
When X = A, Y should only display 1, 2, 3
When X = B, Y should only display 4, 5, 6 

How to do this?
Should I create 2 catalog client scripts for X = A and X = B?

Please help!

Thank you!

 

 

1 ACCEPTED SOLUTION

Shrutika Surwad
Kilo Guru

Hey,

I did same requirement on my instance and it is working right

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

g_form.addOption('variable_y',1,1);
g_form.addOption('variable_y',2,2);
g_form.addOption('variable_y',3,3);
	
g_form.removeOption('variable_y',4,4);
g_form.removeOption('variable_y',5,5);
g_form.removeOption('variable_y',6,6);

}

else if(newValue == 'b'){
g_form.addOption('variable_y',4,4);
	g_form.addOption('variable_y',5,5);
	g_form.addOption('variable_y',6,6);
	
g_form.removeOption('variable_y',1,1);
g_form.removeOption('variable_y',2,2);
g_form.removeOption('variable_y',3,3);

}

   //Type appropriate comment here, and begin script below
   
}

find_real_file.png

find_real_file.png

 

Thanks,

Shrutika

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

only 1 onChange client script on Variable X should suffice.

Sample script below

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

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

if(newValue === '')

g_form.clearOptions('variable_y');

return;

}

g_form.clearOptions('variable_y');

if(newValue == 'A'){

g_from.addOption('variable_y',1,1);

g_from.addOption('variable_y',2,2);

g_from.addOption('variable_y',3,3);

}

else if(newValue == 'B'){

g_from.addOption('variable_y',4,4);

g_from.addOption('variable_y',5,5);

g_from.addOption('variable_y',6,6);

}

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I updated your code to this:

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

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

if(newValue === '')

g_form.clearOptions('variable_y');

return;

}

g_form.clearOptions('variable_y');

if(newValue == 'A'){

g_from.addOption('variable_y',1,1);

g_from.addOption('variable_y',2,2);

g_from.addOption('variable_y',3,3);

g_from.removeOption('variable_y',4,4);

g_from.removeOption('variable_y',5,5);

g_from.removeOption('variable_y',6,6);

}

else if(newValue == 'B'){

g_from.removeOption('variable_y',1,1);

g_from.removeOption('variable_y',2,2);

g_from.removeOption('variable_y',3,3);

g_from.addOption('variable_y',4,4);

g_from.addOption('variable_y',5,5);

g_from.addOption('variable_y',6,6);

}

}

 

It works when a value is selected (ex. A). But when another value is selected without refreshing the form (ex. B), no option is displayed.

Hi ceraulo,

Please correct name g_form.

Thanks,

Priyanka

Hi,

one more addition; when value changes you should clear previous options and then add the required options

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

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

if(newValue === '')

g_form.clearOptions('variable_y');

return;

}

g_form.clearOptions('variable_y');

if(newValue == 'A'){

g_form.clearOptions('variable_y');

g_from.addOption('variable_y',1,1);

g_from.addOption('variable_y',2,2);

g_from.addOption('variable_y',3,3);


}

else if(newValue == 'B'){

g_form.clearOptions('variable_y');

g_from.addOption('variable_y',4,4);

g_from.addOption('variable_y',5,5);

g_from.addOption('variable_y',6,6);

}

}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader