OnChange Catalog Client Script - doesn't work if dependent value changes

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2017 09:51 AM
I really have two questions about something I'm trying to do.
On a Record Producer, I'm trying to make the Subcategory (u_subcategory) variable dependent on the Category (category) variable. Each of them point to the Time Card table to get the choices.
The below script works, but only if the Category value only changes once. If I choose Category Architecture (aeutilized), the right Subcategories are displayed, but if I change the Category after that to On Site Support (onsite), no Subcategories are displayed. So the script only runs the first time the Category is changed, apparently. How do I make it work every time the Category changes without having to reload the form?
Ignore the commented stuff, I'll get to that later....
Catalog Client Script: Control Subcategories
Type: onChange
Variable name: category
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'aeutilized')
{
//var cList = new GlideRecord('sys_choice');
//cList.addQuery('name','time_card');
//cList.addQuery('element','u_subcategory');
//cList.addQuery('dependent_value','onsite');
//cList.query();
//while (cList.next())
//{
//g_form.removeOption('variables.u_subcategory', cList.value, cList.label);
//}
g_form.removeOption('variables.u_subcategory','acme');
g_form.removeOption('variables.u_subcategory','microsoft');
}
else if (newValue == 'onsite')
{
//var cList = new GlideRecord('sys_choice');
//cList.addQuery('name','time_card');
//cList.addQuery('element','u_subcategory');
//cList.addQuery('dependent_value','onsite');
//cList.query();
//while (cList.next())
//{
//g_form.removeOption('variables.u_subcategory', cList.value, cList.label);
//}
g_form.removeOption('variables.u_subcategory','vendorrel');
g_form.removeOption('variables.u_subcategory','prodeval');
g_form.removeOption('variables.u_subcategory','netsup');
g_form.removeOption('variables.u_subcategory','archdesign');
g_form.removeOption('variables.u_subcategory','testdev');
g_form.removeOption('variables.u_subcategory','security');
g_form.removeOption('variables.u_subcategory','syssup');
g_form.removeOption('variables.u_subcategory','lms');
g_form.removeOption('variables.u_subcategory','documentation');
g_form.removeOption('variables.u_subcategory','iaas');
}
}
Now... if I can get that to work, I would rather write this script so that it's dynamic and I don't have to change it every time I add a subcategory. Following the article here: http://wiki.servicenow.com/index.php?title=Adding_Dependent_Variables#gsc.tab=0 I tried the parts of the script that are commented instead of the individual 'removeOption' statements. I can't get that to work at all. Can anyone tell me what is wrong there?
Thanks for any assistance....
Karla

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2017 09:56 AM
why have you given variables.u_subcategory? Is that a field name defined in that way?
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2017 11:42 AM
Because I'm trying to control a record producer variable...? I was just following an example that I saw on the community somewhere. I took that out of my working script and it still works (except that I still have the same issue where the Category can only change once), so I guess it isn't necessary.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2017 09:58 AM
You can make the fields as lookup choice and use reference qualifier instead.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2017 10:00 AM
Well first once you remove an option its gone so you have to add them all back which I do not see. So I would guess its working just does not look like it is.
Which is probably why it also did not work when following the wiki, your code does not add the options when the value changes.