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

Hi Hemanth,

So that is the issue. Since you must be trying to set some subcategory value which is not relevant to category already present it is not allowing

you can try one thing; remove all the options from subcategory and then based on the group and then based on category populate the options 1 by 1

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

I am already working on the same. Let me see how it goes.

Hi Hari,

Did you find any solution for the issue, I am also facing a similar issue where Client Script is conflicting with Dependent Field, so was interested what solution you applied to fix this issue?

Regards,

Debjit

Sanket Khabiya
Kilo Sage

Hi,

Please try with below solution:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	var getGroup,getSubCat;
	getGroup = g_form.getValue('assignment_group');
	alert("getGroup: " + getGroup);
	alert("getCat: " + newValue);

	if (isLoading || newValue === '') 
	{

		// 		here you have to remaove all the options from subcategory
		// 		g_form.removeOption('subcategory','........
		// 							.
		// 							.
		// 							.
		// 							.
		// 							.
		// 							.

	}

	else if(newValue == 'Application' && getGroup == 'e717093b786b5c00910063ad31c0bff5')
	{
		alert("Inside if of Application");
		g_form.removeOption('subcategory', 'MS Office');
		g_form.removeOption('subcategory', 'Teams');
		g_form.removeOption('subcategory', 'Outlook (email)');

		//here you have to add all the options in subcategory which you want to show when category is "application" and group is "e717093b786b5c00910063ad31c0bff5"
		//g_form.addOption('subcategory','');
	}

	else if(newValue == 'Hardware' && getGroup == 'e717093b786b5c00910063ad31c0bff5')
	{
		alert("Inside if of Hardware");
		g_form.removeOption('subcategory','Handheld wireless device');
		g_form.removeOption('subcategory','Laptop');
		g_form.removeOption('subcategory','Workstation');

		//here you have to add all the options in subcategory which you want to show when category is "Hardware" and group is "e717093b786b5c00910063ad31c0bff5"
		//g_form.addOption('subcategory','');
	}

	else if(newValue == 'Network' && getGroup == 'e717093b786b5c00910063ad31c0bff5')
	{
		alert("Inside if of Network");
		g_form.removeOption('subcategory','Wireless');

		//	here you have to add all the options in subcategory which you want to show when category is "Network" and group is "e717093b786b5c00910063ad31c0bff5"
		//g_form.addOption('subcategory','');
	}

	else if(newValue == 'Software' && getGroup == 'e717093b786b5c00910063ad31c0bff5')
	{
		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');

		//		here you have to add all the options in subcategory which you want to show when category is "Software" and group is "e717093b786b5c00910063ad31c0bff5"
		//g_form.addOption('subcategory','');
	}

	else
	{
		//getCat = g_form.getValue('category');

		if(newValue == 'Application')
		{
			alert("Inside if of Application when group changes");
			// 					here you have to remove all the options which you don't wanted to show
			// 					g_for.removeOption('subcategory','........
			// 									   .
			// 									   .
			// 									   .
			// 									   .

			g_form.addOption('subcategory', 'MS Office');
			g_form.addOption('subcategory', 'Teams');
			g_form.addOption('subcategory', 'Outlook (email)'); 	
		}

		else if(newValue == 'Hardware')
		{
			alert("Inside if of Hardware when group changes");
			// 					here you have to remove all the options which you don't wanted to show
			// 					g_for.removeOption('subcategory','........
			// 									   .
			// 									   .
			// 									   .
			// 									   .
			g_form.addOption('subcategory','Handheld wireless device');
			g_form.addOption('subcategory','Laptop');
			g_form.addOption('subcategory','Workstation');
		}

		else if(newValue == 'Network')
		{
			alert("Inside if of Network when group changes");
			// 					here you have to remove all the options which you don't wanted to show
			// 					g_for.removeOption('subcategory','........
			// 									   .
			// 									   .
			// 									   .
			// 									   .
			g_form.addOption('subcategory','Wireless');
		}

		else if(newValue == 'Software')
		{
			alert("Inside if of Software when group changes");
			// 						here you have to remove all the options which you don't wanted to show
			// 						g_for.removeOption('subcategory','........
			// 										   .
			// 										   .
			// 										   .
			// 										   .
			g_form.addOption('subcategory','Driver');
			g_form.addOption('subcategory','MS Office');
			g_form.addOption('subcategory','Infopath');
			g_form.addOption('subcategory','RMS');
		}	
	}
}

}

Regards,

Sanket