- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2022 06:11 PM
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');
}
}
thanks
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 03:12 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-06-2023 12:54 AM
Hi, am trying to achieve the same functionality but not working in my case, but it's not working. Can you please suggest me how to proceed on this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') { return;
}
var category = g_form.getValue('category');
if (category == 'EUS') { setInterval(function(){ g_form.removeOption('subcategory', 'local');}, 1000); } }