Removing choices using client script

AmritaT
Tera Expert

Hello experts,

I have a use case as given below-

1. Field 1 - Customer: type a) Internal b) external

2. Field 2 - Location

Location choice - USA, UK, Germany

For b) external, show only UK and hide rest.

I think I cannot use UI policy. Trying my hands on Client Script, however I'm not doing it right.

Please suggest. Thanks!

5 REPLIES 5

Saiganeshraja
Kilo Sage
Kilo Sage
Use g_form.removeoption() refer below https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options/ Mark correct and helpful

Musab Rasheed
Tera Sage
Tera Sage

Hi @AmritaT ,

UI policy might create conflict if you have 'None' option and not defaulting to either of one choice, so try below onchange client script and modify accordingly. Don't forgot to mark my answer as correct if that helps.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	
	if(newValue == 'internal')
		{
			g_form.clearOptions('location');
			g_form.addOption('location', 'choice value', 'choice label');
		}
	else if(newValue == 'external')
		{
			g_form.clearOptions('location');
			g_form.addOption('location', 'choice value', 'choice label');
			g_form.addOption('location', 'choice value', 'choice label');
		}
	else
		{
			g_form.clearOptions('location');
			//add none
			g_form.addOption('location', '--None--', '--None--');
		}

   //Type appropriate comment here, and begin script below
   
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

Hi Musab,

Thanks for your response.

The customer information is a check-box. For eg. Customer - this field refers to company list.

There is a check-box for 'external' user. The field says, if customer not found, click here...

In this case, what should be the newValue?

if(newValue == 'internal')

Should it be if(newValue == 'true') as it's a checkbox?

 

Thanks!

Hi @AmritaT ,

I will just give you hint, if you are confused about backend value then always try to show them as Alert in your case just add alert as 

alert(newValue);

This will backend value of internal or external , also in case you want to know onload script then use like this.

alert(g_form.getValue('name of field');

Don't forgot to mark my answer as correct if that helps.

Please hit like and mark my response as correct if that helps
Regards,
Musab