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

Hi,

I'm glad my initial reply was Helpful and assisted you to change your script. Please mark that reply as Helpful, if so.

As far as your onChange not working...

Please add alert/console log troubleshooting statements so you can see what is going on. Also, please check your UI settings to ensure if you're trying to do this on the Portal, that the UI type is set to the appropriate value. Also ensure that the onChange field that it's set to execute on...is in fact your category field.

At this point, we can't really assist beyond suggesting things we can think of. You haven't supplied a screenshot of your actual client script form/settings so we can verify what you have.

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


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

Hi 

 

this is to work on plataform not Portal. 

 

find_real_file.png

 

 

find_real_file.png

 

 

choice list - subcategory

find_real_file.png

 

I have added messages and I didn't find why is not working 

 

Thanks

 

I have got It working using setInterval Here is the code I've used : 

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') { setInterval(function(){ g_form.removeOption('u_subcategory', 'teste');}, 1000); } }

Hi,

Great 🙂

As I had mentioned above, in my initial reply, there's a timing concern you'd have to take in to account and I had explained how the system finds those dependencies, etc.

"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)."

If my reply above helped guide you Correctly, please mark it as Correct.

Thanks and take care!

 


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

sidkak_28
Tera Contributor

This actually helped to solve a similar issue of mine. Thanks.