How to set the dropdown option based on previous reference field value?

Vijayr119
Tera Contributor

I have 2 fields from the form.

1. Model Category (Reference field)

2. Model Sub Category (Choice field)

 

We have below fields from the Model category value:

Computer
Mobile Device
Printer
Peripheral
Display hardware

 

Now, I want to list Model Sub Category (Choice field) based on the selection from Model Category (Reference field) field.

 

Ex: If the user selects Computer as a Model Category (Reference field) then I should see only below dropdown values from Model Sub Category (Choice field)

Desktop
Laptop
Gaming Console

 

How can I achieve this with the client script?

 

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Vijayr119 

 

You can write onChange Client Script on Model Category field, and than using g_form.addOption() and g_form.removeOption() function, you can add drop down options for your Model Sub Category field.

 

Find sample code below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var modCat = g_form.getValue('model_category');

if(modCat == '<sysid of Comupter>'){

g_form.addOption('model_sub_category', 'desktop', 'Desktop');
g_form.addOption('model_sub_category', 'laptop', 'Laptop');
g_form.addOption('model_sub_category', 'gaming_console', 'Gaming Console');
g_form.removeOption('model_sub_category','mobile');
g_form.removeOption('model_sub_category','ipad');
} 
//.....

 

Please refer below link to know more about this functions:

https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options....

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

1 REPLY 1

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Vijayr119 

 

You can write onChange Client Script on Model Category field, and than using g_form.addOption() and g_form.removeOption() function, you can add drop down options for your Model Sub Category field.

 

Find sample code below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var modCat = g_form.getValue('model_category');

if(modCat == '<sysid of Comupter>'){

g_form.addOption('model_sub_category', 'desktop', 'Desktop');
g_form.addOption('model_sub_category', 'laptop', 'Laptop');
g_form.addOption('model_sub_category', 'gaming_console', 'Gaming Console');
g_form.removeOption('model_sub_category','mobile');
g_form.removeOption('model_sub_category','ipad');
} 
//.....

 

Please refer below link to know more about this functions:

https://servicenowguru.com/scripting/client-scripts-scripting/removing-disabling-choice-list-options....

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023