Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Change field options based on another field

Staxed
Tera Guru

I have found a few answers to similar questions in the past, but for the life of me I can't get this working and am hoping someone might be able to give me example code:

Not sure if I should be using a client script or ui policy for it.

The form is for "Table2" which is extending "Table1" if it matters, the fields are on "Table1"

"Field1" has 4 options, "Field2" has 9 options.  So I'd like to filter out "Field2" based on the value of "Field1".

 Edit: I got it working, but am wondering if there is a way to make this shorter (clearing and adding options instead of removing options would be less rows I think, but can't figure out how to clear the options and then add new ones).  I'd only have 2 add 2 instead of removing 6.

 

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

if(newValue=='Division')
{
g_form.removeOption('u_hr_location','Location1','Location1');
g_form.removeOption('u_hr_location','Location2','Location2');
g_form.removeOption('u_hr_location','Location3','Location3');
g_form.removeOption('u_hr_location','Location4','Location4');
g_form.removeOption('u_hr_location','Location5','Location5');
g_form.removeOption('u_hr_location','Location6','Location6');
} 
}
1 ACCEPTED SOLUTION

Saurav11
Kilo Patron
Kilo Patron

Hello,

To add options please use the below syntax:-

g_form.addOption('fieldname','fieldlame','fieldlabel',order);

To clear the variable:-

g_form.clearOption('variable', 'fieldname');

Please mark answer correct/helpful based on Impact.

 

View solution in original post

2 REPLIES 2

Saurav11
Kilo Patron
Kilo Patron

Hello,

To add options please use the below syntax:-

g_form.addOption('fieldname','fieldlame','fieldlabel',order);

To clear the variable:-

g_form.clearOption('variable', 'fieldname');

Please mark answer correct/helpful based on Impact.

 

that worked after I changed g_form.clearOption to g_form.clearOptions

Thank you!