How to use Category and Subcategory variable

jituksingh
Kilo Guru

Hi All,

I have a catalog form where I have two select box variable called Category and Subcategory. Category and Subcategory has various choice. My requirement is when a choice is selected in Category only a specific set of choice should be available for Subcategory. Example below.

Category has three options

find_real_file.png

Subcategory has following options

find_real_file.png

But when Audio issue is selected under Category only following options should be available under subcategory

find_real_file.png

Other category and subcategory as below

find_real_file.png

Catalog Form

find_real_file.png

Any help is highly appreciated. Thanks in advance!

Regards,

Jitendra Singh

 

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Let me know if that answered your question. If so, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list

 

- Pradeep Sharma

ryan_pope
Mega Guru
Variables Requirements:
There is a dependency on their being some mechanism for the first variable value being reference-able in relation to the second.
    Ex1: Variable 1: Location reference variable, Variable 2: Building reference variable, where building records have a reference to the Location table.
    Ex2: Variable 1: Select box of categories, Variable 2: Lookup select box of subcategories. (My approach for these is, rather than add them a select box using the question_choice table, I add them directly to the sys_choice table, which has a field OOB for "Dependent Value". The "Lookup value field" should be "Sys ID", and the "Lookup label field(s)" should be "label". For each subcategory, the dependent value should be the *value* (not label) of the corresponding category).
                
 
Dynamic Filtering Setup:
  1. Write a script include function that takes a parameter (which will be the first variable's value) to query the reference table of the second variable, and return a stringified encoded query based on that query.
Ex:
var VarDependencyUtils = Class.create();
VarDependencyUtils.prototype = {
    initialize: function() {
    },
    catSubCatFilter: function(categoryValue) {
        
        var choices = new GlideRecord('sys_choice');
        choices.addQuery('name', 'question_choice');
        choices.addQuery('element', 'subcategory');
        choices.addQuery('dependent_value', categoryValue);
        choices.query();
        
        var choicesArr = [];
        while (choices.next()){
            choicesArr.push(choices.sys_id + '');
        }
        
        var returnQuery = 'sys_idIN' + choicesArr.join();
        return returnQuery;
        
    },
    type: 'VarDependencyUtils'
};
 
  1. We need to tell the system that the first variable has a dependency on it. To do this, in the "Default Value" tab (at least this is usually where it is), we need to add the following to the "Variable attributes" field:
ref_qual_elements=subcategory
 
  1. For the second variable, we also need to tell the system that this variable is dependent on the first. Following the same navigation as the previous step, add the following to the "Variable attributes" field:
ref_qual_elements=category
 
  1. Finally, in the "Type Specifications" tab (again, this is usually where this field is), we need to add the following to the "Reference qual" field:
javascript:new VarDependencyUtils().catSubCatFilter(current.variables.category);
 
Voila! You now have a variable that is dependent on another! 
 

Thank you so much for this it worked great with one exception for my case. I am trying to use this from a unauthenticated record producer. When I try to use this the sub category comes up empty. Any thoughts on how I can get public access to the sys_choice or what ever is needed to work?