Dependent field and Client script

Udit5
Tera Contributor

Hi,

 

I have 2 fields, category and subcategory, Subcategory is a dependent field on category.
on the basis of category options, subcategory options will be visible.

Now I need to add one more choice in subcategory with the help of onchange client script.

I have written code like 

var cat = g_form.getValue('category');

if(cat == 'A' || cat == 'B'){

g_form.clearOptions('subcategory);

g_form.addOptions('subcategory', '1', 'ABC');

}

 

This script is not working as we already have some dependence on the category field.

As soon as I select A or B in category field, it shows ABC value in subcategory for miliseconds and then again back to the dependency choice options present in dictionary.

Also , does clearOption method has to be used before addOptions method? because if i remove clearOptions then it does not even work for miliseconds as well.

Any suggestions on this? Please help.

I CANNOT add another option is subcategory field itself-  this is important.

I have to use onchange client script for this.

Need advice on this.

 

2 REPLIES 2

Community Alums
Not applicable

Hi @Udit5 

 

I would suggest using the OOB dependent field functionality. Please explore the below links:

 

https://docs.servicenow.com/en-US/bundle/tokyo-platform-administration/page/administer/field-adminis...

 

https://www.servicenow.com/community/developer-forum/how-to-configure-dependent-values/m-p/2004166

 

Please mark my answer as correct if this helps

prasanna11
Mega Guru

Facing the same issue, were you able to get a solution on this. 

 

I tried to setTimeout, but that sometime works and sometimes doesnt.