Client Script - g_form.removeOption

hongsok
Tera Contributor

Hello all,

I have created a catalog item and there is a requirement to show the option in the select box based on the variable.

The variable name are "model_category" & "form_factor". I have created the following OnChange client script and it work but the problem is if the requester change to other option in the model_category the form_factor does not updated.

if (g_form.getValue('model_category') == 'computer') {
    g_form.removeOption('form_factor', 'smart_phone');
    g_form.removeOption('form_factor', 'tablet');

} else if (g_form.getValue('model_category') == 'mobile_device') {

    g_form.removeOption('form_factor', 'desktop');
    g_form.removeOption('form_factor', 'laptop');

}

Regards,

Hong

 

1 ACCEPTED SOLUTION

Voona Rohila
Kilo Patron
Kilo Patron

Hi hongsok

since you are the options the options need to be added again.

Try this

g_form.clearOptions('form_factor');
g_form.addOption('form_factor','','-- None --',100)
if (g_form.getValue('model_category') == 'computer') {
    g_form.removeOption('form_factor', 'smart_phone');
    g_form.removeOption('form_factor', 'tablet');

    g_form.addOption('form_factor','laptop','Laptop',100);
    g_form.addOption('form_factor','desktop','Desktop',200);


} else if (g_form.getValue('model_category') == 'mobile_device') {

    g_form.removeOption('form_factor', 'desktop');
    g_form.removeOption('form_factor', 'laptop');

     g_form.addOption('form_factor','smart_phone','Smart Phone',300);
    g_form.addOption('form_factor','tablet','Tablet',400);

}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

are you using correct choice value during removal of option?

Regards
Ankur

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

Yes. Rohila's script is working.

Hi,

Nice to know.

1) clear the options when onchange works

2) then add options which you require instead of removing

Regards
Ankur

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

Voona Rohila
Kilo Patron
Kilo Patron

Hi hongsok

since you are the options the options need to be added again.

Try this

g_form.clearOptions('form_factor');
g_form.addOption('form_factor','','-- None --',100)
if (g_form.getValue('model_category') == 'computer') {
    g_form.removeOption('form_factor', 'smart_phone');
    g_form.removeOption('form_factor', 'tablet');

    g_form.addOption('form_factor','laptop','Laptop',100);
    g_form.addOption('form_factor','desktop','Desktop',200);


} else if (g_form.getValue('model_category') == 'mobile_device') {

    g_form.removeOption('form_factor', 'desktop');
    g_form.removeOption('form_factor', 'laptop');

     g_form.addOption('form_factor','smart_phone','Smart Phone',300);
    g_form.addOption('form_factor','tablet','Tablet',400);

}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP