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

Mike Patel
Tera Sage

try below

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

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

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

This works! 

Thank you.

You are welcome

Regards
Ankur

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