Add/Remove options from select box

ceraulo
Mega Guru

Hello!

In a catalog item, i have 2 variables, VAR A and VAR B. 

Var A has 2 options - Option 1 and Option 2
VAR B has 2 options, Option 1 and Option 2

The requirement is if the user selects VAR A = Option 1, VAR B will only show Option 2.

//  newValue is VAR_A

if (newValue == 'Option 1') 
        g_form.removeOption('VAR _B', 'Option 1');

    
else if (newValue == 'Option 2') 
	g_form.removeOption('VAR _B', 'Option 2');

    

This works if I change VAR A to Option 1.
BUT when I change the VAR A selection to Option 2, all of the VAR B values disappear.

I used  an On Change catalog client script for this.

Please help!

Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

please try this updated script

1) first clear all the options

2) then add respective option

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

g_form.clearOptions('VAR _B');

if (newValue == 'Option 1') {
	g_form.addOption('VAR _B', 'Option 2', 'Option 2 Label');
}
if (newValue == 'Option 2') {
	g_form.addOption('VAR _B', 'Option 1', 'Option 1 Label');
}

}

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi @Ankur Bawiskar 

if we clear the options then it is removing None as well,

if the var_b is mandatory even if it is none(added through script) which allows to user submit the form

 

Hi,

 

This help me a lot. However when the newValue changes, the --None-- option disappear, and i need that always visible and the field mandatory when is select, can you help?

 

Thanks in advance

Marisa