Populate choices on a field based on choices from two other fields

divyakosaraju
Giga Expert

 

Hi,

I have a field name solution which is dependent on two other choice fields . below is the scenario

 

field 1 - Choice 1, choice 2, choice 3

field 2 -choice a, choice b, choice c, choice d

solution - prod , dev, QA, training ,POC, sandbox etc.,.

 

now based on field 1 and field 2 , I need to populate solution choices

 

if field 1 is choice 1 && field 2 is  either choice a||choice b||choice d, then solution choices should be PROD, DEV and QA

if field 1 is choice 3 && field 2 is  choice c||choice d, then solution choices should be PROD, DEV, sandbox, training and QA

 

 

I tried client script but is not working. I am not sure if decision table works as I cannot select my field choices of that table as a result

Please kindly help

 

My client script is Onchange on field 1

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

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

return;

}

 

var f1 = g_form.getValue('field 1');

var f2 = g_form.getValue('field 2');

 

if (newValue == 1 )

{

if (f2 == "choice a" || f2 == "choice b" || f2 =="choice d" ){

g_form.clearOptions('solution'); 

g_form.removeOption('solution','3','Test');

g_form.removeOption('solution','5','Training');

g_form.removeOption('solution','13','POC');

}

 

}

 

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

For your testing, just know that since you have a script that runs onChange of field 1 and depends on field 2, that field 2 must be populated before field 1.  Once you get this working add an exclusion to not run if field 2 is empty, or whatever fits your use case, then create a similar script onChange of field 2 so that no matter what order the 2 fields are populated in, the script will still run.

 

As far as why it's not working, you'll have to add some alerts on f1 and f2 which will confirm that those field names are correct, and the value you are testing for (newValue/f1 and f2) is expected.  It doesn't make sense to clearOptions then removeOptions for individual choices, so do one or the other as clearOptions will remove all options.  If you take this line out, make sure 'Test' is the value of the choice record, not the label.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

For your testing, just know that since you have a script that runs onChange of field 1 and depends on field 2, that field 2 must be populated before field 1.  Once you get this working add an exclusion to not run if field 2 is empty, or whatever fits your use case, then create a similar script onChange of field 2 so that no matter what order the 2 fields are populated in, the script will still run.

 

As far as why it's not working, you'll have to add some alerts on f1 and f2 which will confirm that those field names are correct, and the value you are testing for (newValue/f1 and f2) is expected.  It doesn't make sense to clearOptions then removeOptions for individual choices, so do one or the other as clearOptions will remove all options.  If you take this line out, make sure 'Test' is the value of the choice record, not the label.

Hi Brad, I tried this route and it is working .Thankyou very much

You are welcome!

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @divyakosaraju 

 

Did you try the data lookup way, create a lookup table,

 

Match the choices and set the results.

 

Have a look ootb priority lookup and try once.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************