How to hide choices of fields

ClayS
Tera Contributor

Hello, I want to hide some choices but don't want to delete them.
Because some choice type fields are configrated to have dependencies.
Ex) [field_1]-choice_a, choice_b, choice_c
      [field_2]-choice_d, choice_e *They will appear if choice_a are selected in [field_1].

Currently there are too many choices in one field and I would like to reduce them.
If you know how to make some of the choices invisible, please help me.
Thank you.

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @ClayS 

To meet the requirement, write an onChange client script on field_1.

onChange Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Do nothing if the form is loading or the field is empty
    }

    // Clear the options in field_2 to reset it
    g_form.clearOptions('field_2');

    // Check the value of field_1 and dynamically add options to field_2
    if (newValue === 'choice_a') {
        // Add the dependent options for choice_a
        g_form.addOption('field_2', 'choice_d', 'Choice D');
        g_form.addOption('field_2', 'choice_e', 'Choice E');
    } else if (newValue === 'choice_b') {
        // Add other dependent options for choice_b if needed
        g_form.addOption('field_2', 'choice_f', 'Choice F');
        g_form.addOption('field_2', 'choice_g', 'Choice G');
    } else {
        // Add a default placeholder if no matching options
        g_form.addOption('field_2', '', '-- Select an option --');
    }
}

 

Note: Update the option value and label as it is configured in your instance.

 Refer the link to learn more about dynamically populating the choices 

Using the g_form.addOption method

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

5 REPLIES 5

dgarad
Giga Sage

Hi @ClayS 

In onChange Client Script: 

 

var value = g_form.getValue('field1');
 if(value==  choice_a){
g_form.addOption('field2', 'choice_d');
g_form.addOption('field2', 'choice_e');

}
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Ankur Bawiskar
Tera Patron
Tera Patron

@ClayS 

you can remove options using this

g_form.removeOption('field_1', 'choiceValue1');

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Juhi Poddar
Kilo Patron

Hello @ClayS 

To meet the requirement, write an onChange client script on field_1.

onChange Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Do nothing if the form is loading or the field is empty
    }

    // Clear the options in field_2 to reset it
    g_form.clearOptions('field_2');

    // Check the value of field_1 and dynamically add options to field_2
    if (newValue === 'choice_a') {
        // Add the dependent options for choice_a
        g_form.addOption('field_2', 'choice_d', 'Choice D');
        g_form.addOption('field_2', 'choice_e', 'Choice E');
    } else if (newValue === 'choice_b') {
        // Add other dependent options for choice_b if needed
        g_form.addOption('field_2', 'choice_f', 'Choice F');
        g_form.addOption('field_2', 'choice_g', 'Choice G');
    } else {
        // Add a default placeholder if no matching options
        g_form.addOption('field_2', '', '-- Select an option --');
    }
}

 

Note: Update the option value and label as it is configured in your instance.

 Refer the link to learn more about dynamically populating the choices 

Using the g_form.addOption method

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Arun kumar Chin
Tera Contributor

ArunkumarChin_0-1739527842568.png

If I select 'Mouse' in the Devices field, only Kerala and Bangalore should be available in the Places field. 

 My code is not working:

 

function onChange(control, oldValue, newValue, isLoading) {
    // if (isLoading || newValue == '') {
    //     return;
    // }
    if (g_form.getValue('devices') == 'Mouse') {
        g_form.removeOption('place', 'Bangalore');
        g_form.removeOption('place', 'Pune');
        g_form.removeOption('place', 'Uttar Pradesh');
    } else if (g_form.getValue('devices') == 'Keyboard') {
        g_form.removeOption('place', 'Hyderabad');
        g_form.removeOption('place', 'Kerala');
        g_form.removeOption('place', 'Pune');
    }
}
 
 
 
can someone help me on this  ?