removeOption and addOption not working

Hari1
Mega Sage

Hi,

I need to remove the choice list values of the "subcategory" field which is dependent on "category" field.

Client script: Onload()

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var getGroup,getCat,getSubCat;
	
	getGroup = g_form.getValue('assignment_group');
	
	alert("getGroup: " + getGroup);
	
	if(getGroup == 'e717093b786b5c00910063ad31c0bff5')
	{
		getCat = g_form.getValue('category');
		
		alert("getCat: " + getCat);
		
		if(getCat == 'Application')
		{
			alert("Inside if of Application");
			g_form.removeOption('subcategory', 'MS Office');
            g_form.removeOption('subcategory', 'Teams');
			g_form.removeOption('subcategory', 'Outlook (email)');
		}
		
		if(getCat == 'Hardware')
		{
			alert("Inside if of Hardware");
			g_form.removeOption('subcategory','Handheld wireless device');
			g_form.removeOption('subcategory','Laptop');
			g_form.removeOption('subcategory','Workstation');
		}
		
		if(getCat == 'Network')
		{
			alert("Inside if of Network");
			g_form.removeOption('subcategory','Wireless');
		}
		
		if(getCat == 'Software')
		{
			alert("Inside if of Software");
			g_form.removeOption('subcategory','Driver');
			g_form.removeOption('subcategory','MS Office');
			g_form.removeOption('subcategory','Infopath');
			g_form.removeOption('subcategory','RMS');
		}
	}
	else
	{
		getCat = g_form.getValue('category');
		
		if(getCat == 'Application')
		{
			alert("Inside if of Application when group changes");
			g_form.addOption('subcategory', 'MS Office');
            g_form.addOption('subcategory', 'Teams');
			g_form.addOption('subcategory', 'Outlook (email)'); 	
		}
		
		if(getCat == 'Hardware')
		{
			alert("Inside if of Hardware when group changes");
			g_form.addOption('subcategory','Handheld wireless device');
			g_form.addOption('subcategory','Laptop');
			g_form.addOption('subcategory','Workstation');
		}
		
		if(getCat == 'Network')
		{
			alert("Inside if of Network when group changes");
			g_form.addOption('subcategory','Wireless');
		}
		
		if(getCat == 'Software')
		{
			alert("Inside if of Software when group changes");
			g_form.addOption('subcategory','Driver');
			g_form.addOption('subcategory','MS Office');
			g_form.addOption('subcategory','Infopath');
			g_form.addOption('subcategory','RMS');
		}	
	}		
}

All the alerts are working fine as per the above condition.

REF::
find_real_file.png

I don't see the removeOption and addOption to be working.

Any help?

8 REPLIES 8

Omkar Mone
Mega Sage

Hi,

The code is just fine. The only changes that was to be done was in the addOption to add the label value.

Even after that no luck.

Hi Hemanth,

Since you said your subcategory is dependent on category field; possibly that might be blocking from adding/removing the options

It seems you are trying to populate subcategory based on category in your script; can you try to remove the subcategory as dependent field on category and check once

Regards

Ankur

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

Hi Ankur,

Can't this be done without removing the dependency field? If we remove it i am able to see that the removeOption and addOption is working fine.

But removing the dependency field will lead to display all the subcategory of the dependent field of all the categories.