Using a Lookup Select Box to Narrow options for Second Lookup Select Box

JamieNow
Tera Contributor

Hello, Developers!

Here's the low down:

  1. I have two lookup select box variables on my catalog item that I want to filter the same table
  2. The first one should filter based on a field we're using as a flag called "type" and it should equal to "category"
  3. The second field should filter the same table with the "type" equal to "topic" AND it should show records from the table that have a "parent" field (reference field for the same table with "name" field as display value) set to the same option selected from the first list select box

Here's what I've tried:

  1. An advanced Reference Qualifier
    • javascript:'parent='+ current.variables.category
    • javascript:'parent='+ current.variables.category.toString()
  2. I tried a script include
    • Code: 
var CategoryReferenceQualifier = Class.create();
CategoryReferenceQualifier.prototype = {
  initialize: function() {},

    getSysIdFromName: function(name) {
      try{
        var categorySysId = '';
        var gr = new GlideRecord('my_custom_table_name'); 
        gr.addQuery('category_name', name);
        gr.query();
        if (gr.next()) {
            categorySysId = gr.sys_id.toString();
        }
        return categorySysId;
       }
       catch (ex) {
         gs.error("Error in getSysIdFromName: " + ex);
         return '';
       }
    },


  getReferenceQualifier: function(type, categoryName) {
      var categorySysId = this.getSysIdFromName(categoryName);
      if (!categorySysId) {
          return '';
      }
      gs.info("Here's the sysID: " + categorySysId);
      gs.info("Here's the type: " + type);
      return 'parent=' + categorySysId + '^type=' + type;
  },

  type: 'CategoryReferenceQualifier'
};
  • Reference Qualifier: javascript: new CategoryReferenceQualifier().getReferenceQualifier(current.Variables.category)

N.B. I  used the .toString() method and the script include just in case the the "parent" reference field I'm trying to filter by needed a sysId as a string. I'm not even sure what the first list selector box data/value would look like. Is there even a way to debug reference qualifiers? I've looked at product documentation and nothing seems to be doing the trick. Please help! 

 

5 REPLIES 5

mahesh009
Mega Guru

Hi for your requirement, If the category variable type is reference, I think there is no need for script include.You can directly provide below code in advanced qualifier

           javascript:'parent='+ current.variables.category
This will fetch all the records with parent as selected category. If you want to add additional quary on type, please use below code 
              javascript:"parent="+ current.variables.category+"^type=topic"
If you have to call script include use below syntax

           javascript: new CategoryReferenceQualifier().getReferenceQualifier(current.Variables.category)



Thanks, and Regards

Mahesh

Please mark this response as correct or helpful if it assisted you with your question.

 

 

I was tryin to keep it as a lookup select box.

Ankur Bawiskar
Tera Patron
Tera Patron

@JamieNow 

in the 2nd one add this

javascript:'parent='+ current.variables.category.toString() + '^type=topic';

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I tried that already. It doesn't work.