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

kchorny
Tera Guru

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

8 REPLIES 8

My script was only removing some of the options, but they were still all being displayed.   I'll try removing all of them, then adding back only the ones I want to see.


I gave up on the 'dynamic' hiding of categories... I tried a lot of different things but could not get that script to work.   If anyone is using it or something similar successfully, please let me know how you did it.



And as for the subcategories not refreshing when the category changes... never got that to work either.   I'm just going to tell my users they have to reload the form if they change categories.


kchorny


If I were you, I would add dependent values for sub category to point to the category. Then change the variables to lookup select box and use reference qualifier instead of a client script


Dave Smith1
ServiceNow Employee
ServiceNow Employee

Agreed with Abhinay - a Reference Qualifier is designed to provide what you seek.



Scripting can also do it, but as you've found - there's quite a maintenance overhead.   Reference Qualifiers are the preferred approach.