Adding, Removing options from client script

PK14
Kilo Guru

Hi Team,

I need to add and remove options based on country (select box) selection. When India, china, singapore, korea and  germany is chosen "USB-A and USB-A Nano" should appear but when other country is chosen only "USB-C" should be displayed

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var loc = g_form.getValue('country');

// removed option by default
    g_form.removeOption('preferred_form', '2'); 
 g_form.removeOption('preferred_form', '1');
    g_form.removeOption('preferred_form', '3');
if (loc == '8938b7111b121100763d91eebc0713ec' || '4d38b7111b121100763d91eebc0713eb' || '4938b7111b121100763d91eebc0713ed' || 'c538b7111b121100763d91eebc0713eb' || '9138b7111b121100763d91eebc0713f6') //India, china, singapore, korea and  germany Sys_ID
{
    g_form.addOption('preferred_form', '1','USB-A');
    g_form.addOption('preferred_form', '3','USB-A Nano');
}
    else if(loc != '8938b7111b121100763d91eebc0713ec' || 
'4d38b7111b121100763d91eebc0713eb' || '4938b7111b121100763d91eebc0713ed' || 'c538b7111b121100763d91eebc0713eb' || '9138b7111b121100763d91eebc0713f6')
{
g_form.addOption('preferred_form', '2','USB-C'); 
}

}

 

Regards,
Priyanka

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So what's not working?

I would suggest this

1) clear all the options when onChange runs and then add the required options

Update as this

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

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

	var locationList = '8938b7111b121100763d91eebc0713ec,4d38b7111b121100763d91eebc0713eb,4938b7111b121100763d91eebc0713ed,c538b7111b121100763d91eebc0713eb,9138b7111b121100763d91eebc0713f6';

	// removed option by default
	g_form.clearOptions('preferred_form'); 
	if (locationList.indexOf(loc) > -1) //India, china, singapore, korea and  germany Sys_ID
	{
		g_form.addOption('preferred_form', '1','USB-A');
		g_form.addOption('preferred_form', '3','USB-A Nano');
	}
	else{
		g_form.addOption('preferred_form', '2','USB-C'); 
	}

}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

Aman Kumar S
Kilo Patron

Modify your script:

if (loc == '8938b7111b121100763d91eebc0713ec' ||  loc == '4d38b7111b121100763d91eebc0713eb' || loc =='4938b7111b121100763d91eebc0713ed' || loc == 'c538b7111b121100763d91eebc0713eb' || loc == '9138b7111b121100763d91eebc0713f6')

do the same in else if block as well

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So what's not working?

I would suggest this

1) clear all the options when onChange runs and then add the required options

Update as this

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

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

	var locationList = '8938b7111b121100763d91eebc0713ec,4d38b7111b121100763d91eebc0713eb,4938b7111b121100763d91eebc0713ed,c538b7111b121100763d91eebc0713eb,9138b7111b121100763d91eebc0713f6';

	// removed option by default
	g_form.clearOptions('preferred_form'); 
	if (locationList.indexOf(loc) > -1) //India, china, singapore, korea and  germany Sys_ID
	{
		g_form.addOption('preferred_form', '1','USB-A');
		g_form.addOption('preferred_form', '3','USB-A Nano');
	}
	else{
		g_form.addOption('preferred_form', '2','USB-C'); 
	}

}

Regards
Ankur

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

Hi Ankur, 

can we not have none option added for this?

Regards,
Priyanka

You can remove the none option in variable itself.

In the type specification tab of your variable there is a check box called 'include none'

 

Regards,
Sumanth