Client Script to update the Choice List on the Subcategory field based on the Company

simonbourne
Kilo Contributor

I have a Category of Software and a list of applications defined in the Subcategory.  These are configured via Choice List.

I want to remove one application from the Subcategory list when a specific Company is selected.

I have done something similar via an OnChange Client Script based on the Company field but I can't get this to work.  This is the script I have....

find_real_file.png

The sys_id is the correct sys_id for the Company.

Where am I going wrong?

7 REPLIES 7

palmen
Tera Guru

Check this in the docs
https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...

Try with

g_form.removeOption('subcategory', 'Sonata');

 

Not sure what you are using var subcategory for, could probably be removed.
Also consider to not use a sys_id in a script, it's best practice to store the sys_id in a property and then use gs.getProperty() to get the sys_id from the property.

This makes the code tidier but does not remove the option from the Choice List.  Can you share the syntax for using the gs.getProperty() command?

Varsha21
Giga Guru

hi  simonbourne,

can you try with this code,make sure that category and application name should as per your requirement,

but hope this will work for you...

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

	var software=g_form.getValue('software');
	if(software=='software1')
		{
			
			g_form.clearOptions('subcatagory');
                        g_form.addOption('subcatagory','application1','app1');
			g_form.addOption('subcatagory','application2','app2');
    		        g_form.addOption('subcatagory','application3','app3');
 			
		}
	
	    if(software=='software2')
		{
	       	    g_form.clearOptions('subcatagory');
                       g_form.addOption('subcatagory','application1','app1');
			g_form.addOption('subcatagory','application2','app2');
    		//g_form.addOption('subcatagory','application3','app3'); if you will not add this it wont be place under it
        }

still if you have any dough,reply for same...

if helpful make it correct or helpful!!

 

Regards,

Varsha.

 

Archana Reddy2
Tera Guru

Hi,

Is there a chance where a single subcategory should be made visible to 2 or more categories? If NO, you can use dependent field on choice list of Subcategory.

Thanks,

Archana