g_form.RemoveOption not working for dependent field

Mara6
Tera Expert

HI all,

I have two fields category and subcategory. Subcategory is dependent of Category, I have a requirement to remove an option when the change type is not standard. I have created an load client script and an onchange client script but is not working.

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('type') != 'standard') {
        g_form.removeOption('u_subcategory', 'teste');
    }


On Change Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (g_form.getValue('type') != 'standard' && newValue == 'Infrastructure Services') {
        g_form.removeOption('u_subcategory', 'testes');
    } else {
        g_form.addOption('u_subcategory', 'teste');
    }
}

find_real_file.png

thanks

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hello,

So the system doesn't grab the dependent subcategories until the category is loaded and each time the category is changed, it goes out to find the dependent subcategories and gets them again, etc.

With that said, for your onLoad, you'd need to assess not only the type field value, but also the category field value since apparently your requirement is more than just "if type is not standard do 'x'"...but that's it's "if type is not standard do 'x' AND category is also 'y' remove 'z' from subcategory".

So that would fix your onLoad that you assess both the type and category appropriately. Keep in mind that you may want to set this onLoad to a higher order number because it takes a few milliseconds for the category to get populated, thus the subcategories to also get populated, for you to then remove an option. Some people have added a client side "timeout" to the function in which the selection would be removed due to this (although it's not really recommended, but that's one way to get it done).

For your onChange, you'd need to not only get the value of type but also category and double-check the value. Is the back-end value for that category really "Infrastructure Services" or is that just the display label? Please ensure it's the back-end value as well. Then remove the option.

As far as adding the option, you're missing a piece where you also need to include the label. Appropriate format would be:

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <optional targetIndex>);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try to remove the option from category as well since subcategory is dependent on category

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (g_form.getValue('type') != 'standard' && newValue == 'Infrastructure Services') {
        g_form.removeOption('category', 'valueHere');
        g_form.removeOption('u_subcategory', 'testes');
    } else {
        g_form.addOption('u_subcategory', 'teste');
    }
}

Regards
Ankur

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

Hi Ankur,

thanks but it didn't work the category disappears but the subcategory still shows two options is not removing one of the options.

The goal is that a specific subcategory of  the category "Infrastructure Services" only displays when the change type is standard for example :

For change type standard >> category = Infrastructure Services subcategory options shoulld be :

  • Testando >>value test
  • Teste1>> value valid

 

For change type normal>> category = Infrastructure Services subcategory options shoulld be only :

  • Testando>> value test

 

Any ideas? Thanks

Allen Andreas
Administrator
Administrator

Hello,

So the system doesn't grab the dependent subcategories until the category is loaded and each time the category is changed, it goes out to find the dependent subcategories and gets them again, etc.

With that said, for your onLoad, you'd need to assess not only the type field value, but also the category field value since apparently your requirement is more than just "if type is not standard do 'x'"...but that's it's "if type is not standard do 'x' AND category is also 'y' remove 'z' from subcategory".

So that would fix your onLoad that you assess both the type and category appropriately. Keep in mind that you may want to set this onLoad to a higher order number because it takes a few milliseconds for the category to get populated, thus the subcategories to also get populated, for you to then remove an option. Some people have added a client side "timeout" to the function in which the selection would be removed due to this (although it's not really recommended, but that's one way to get it done).

For your onChange, you'd need to not only get the value of type but also category and double-check the value. Is the back-end value for that category really "Infrastructure Services" or is that just the display label? Please ensure it's the back-end value as well. Then remove the option.

As far as adding the option, you're missing a piece where you also need to include the label. Appropriate format would be:

g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <optional targetIndex>);

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen, thanks for the tips.

I've fixed the on load script using your tips.

About the onChange I'm still in doubt and yes the category value is really Infrastructure Services it was created with this name and value by my colegue

my scripts are like this now :

OnLoad script :

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('type') != 'standard' &&  g_form.getValue('category') == 'Infrastructure Services') {
        g_form.removeOption('u_subcategory', 'teste');
    }
}



OnChange script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

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

    if (typ != 'standard' && cat == 'Infrastructure Services') {
        if (newValue == 'Infrastructure Services') {
            g_form.removeOption('u_subcategory', 'teste');
        } else {
            g_form.addOption('u_subcategory', 'valid', 'Teste1');
        }
    }
}

the OnChange script is still not working it doesn't remove the subcategory option with value teste

 

Thanks in advance for the help