alan_lowrance
Mega Guru

For those of you working with Category + Subcategory fields, you know how much of a pain it was to initially set them up.   Your Category choices are defined in the sys_choice table by table, element ("category"), value... and then you create other entries for table, element ("u_subcategory"), value, dependent value.   Then you relate which thing it's looking to for that dependent value on the dictionary entry for the subcategory item.

Anyways, once you have a Category + Subcategory, if you want those to show up and actually filter the Subcategory down dynamically on a Record Producer, you'll need to use AJAX in client scripts to make server calls since a Record Producer usually only submits info to the server upon submission.

The category variable on the Record Producer should work fine without any tweaking cause it's looking at the correct table & element in the choice list table.   The problem arises when you look at the subcategory and it shows ALL choices because it doesn't dynamically query the choices against the dependent value.

Stay with me, so you can make a reference qualifier on the Subcategory variable to limit the choices based on a dependency... but since the record producer isn't a real "living record" yet you have to force it to update.   Once the variables are attached to a real record, this reference qualifier should work normally so your client script isn't going to need to run on Requested Items or Catalog Tasks or anything... just in the Catalog Item View.

The Subcategory variable (lookup select box) should be looking at the sys_choice table and have a reference qualifier similar to:

javascript:'name=incident^element=u_subcategory^dependent_value=' + current.variables.category

Then an onChange client script that runs whenever category is changed will feed variables to a script include and you might want to use this same script include for multiple tables so lets make the script include first:

var DenburyAjax = Class.create();

DenburyAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getSubcategories: function() {

      var table = this.getParameter('sysparm_table');

      var element = this.getParameter('sysparm_element');

      var category = this.getParameter('sysparm_category');

   

      var choice = new GlideRecord('sys_choice');

      choice.addQuery('name',table);

      choice.addQuery('element',element);

      choice.addQuery('dependent_value',category);

      choice.orderBy('sequence');

      choice.query();

    while(choice.next()) {

        if (!choice.inactive) {

                  var item = this.newItem("item");

                        item.setAttribute('label', choice.label);

                        item.setAttribute('value', choice.value);

        }

    }

}

   

});

Make sure you check the "Client Callable" box.

Now to feed the category value to that and build the choices on subcategory, your catalog client script should run onChange of the category variable and only the Catalog Item View:

function onChange(control, oldValue, newValue, isLoading) {

  if ((isLoading) || (newValue == ''))

  return;

  var aj = new GlideAjax("DenburyAjax");

  aj.addParam("sysparm_name", "getSubcategories");

  aj.addParam("sysparm_table", "incident");

  aj.addParam("sysparm_element","u_subcategory");

  aj.addParam("sysparm_category", newValue);

  aj.getXML(getResponse);

}

function getResponse(response) {

  var items = response.responseXML.getElementsByTagName("item");

  g_form.clearOptions('u_subcategory'); //has to clear previous buildout  

  g_form.addOption('u_subcategory','','-- None --');

  for (var i = 0; i < items.length; i++) {

    var item = items[i];

    var value = item.getAttribute("value");

    var label = item.getAttribute("label");

    g_form.addOption('u_subcategory', value, label);

  }

}

Make sense? Any questions?

13 Comments
Jaskaran Walia
Kilo Guru

benn23 or huwaldsmeets - The none issue that keep on adding none to sub category when category changes. I am facing the same issue. Do you know if its a bug in SN or did you find a workaround? I am Helsinki version.


huwaldsmeets
Kilo Contributor

Hi Jaskaran,


Back then (end of March) I did find a post somewhere suggesting it was a bug, but in our current version (helsinki-03-16-2016__patch9-hotfix0a) I can't reproduce the issue anymore. Might also have to do with my field type: I switched using a Reference field instead of a Lookup Select Box. It gives the same functionality, but instead of 'None' the field is 'blank' in the service portal what kind of looks nicer in my opinion.


Jaskaran Walia
Kilo Guru

Thanks Huwald! Yeah, seems to be known error and HI request raised already for the issue! I have too now moved from lookup selectbox to reference field! Thanks a million for your reply.