Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Too many choices in catalog item variable

Mike Hashemi
Kilo Sage

I am trying to create a catalog item. For the first variable, I need the user to select from a list of specific CMDB classes. I think the Lookup Multiple Choice variable type is the best option and I have the following settings:

 

  • Lookup from table: sys_db_table
  • Lookup value field: Label
  • Unique values only: true
  • Reference qualifier: javascript:new PhoneSystemClassQualifier().getPhoneSystemClasses();

The associated script include looks like: 

var PhoneSystemClassQualifier = Class.create();
PhoneSystemClassQualifier.prototype = {
    initialize: function() {},

    getPhoneSystemClasses: function() {
        var classes = [
            'u_cmdb_ci_five9_phone_system',
            'u_cmdb_ci_kandy_business_solution_phone_system',
            'u_cmdb_ci_microsoft_teams_phone_system',
            'u_cmdb_ci_synoptek_efax'
        ];

        var gr = new GlideRecord('sys_db_object');
        gr.addQuery('name', classes);
        gr.query();

        var sysIds = [];
        while (gr.next()) {
            sysIds.push(gr.get('sys_id'));
        }

        if (sysIds.length === 0) {
            return 'sys_idISEMPTY';
        } else {
            return 'sys_idIN' + sysIds.join(',');
        }
    },

    type: 'PhoneSystemClassQualifier'
};

 

When I go "Try It", SN is showing all classes in the CMDB. What am I missing?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mike Hashemi 

Why not have a select box i.e. drop down with 4 choices?

if not then why not apply simple filter condition on that variable itself rather than script include and ref qualifier?

AnkurBawiskar_0-1735968735189.png

 

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

View solution in original post

6 REPLIES 6

DrewW
Mega Sage

I feel like I'm missing something.  Why not just use a Choice var and just add the 4 selections manually?  I don't see the point in all the complexity for 4 values.

 

Try this for the refqual

javascript: new PhoneSystemClassQualifier().getPhoneSystemClasses(); 

 

I would also run your code as a background script and see what it returns.

I started with "Multiple Choice" (there is no "Choice" option) but could only get it to accept three options. After that, I started thinking that these four classes are extended from a common class and there can be others. I started with a script that returns children of the common class. I was getting all of the classes, so I stepped back to this simpler script.

 

I put the script include back to:

 

var PhoneSystemClassQualifier = Class.create();
PhoneSystemClassQualifier.prototype = {
    initialize: function() {},

    getPhoneSystemClasses: function() {
        var answer = '';
		var parentClass = 'cb76637947905210a2a26d37e26d43d5'; // Parent class sys_id

		var gr = new GlideRecord('sys_db_object');
		gr.addQuery('super_class', parentClass);
		gr.query();

		var sysIds = [];
		while (gr.next()) {
			//gs.info(gr.label);
			sysIds.push(gr.sys_id.toString());
		}

		if (sysIds.length > 0) {
			answer = 'sys_idIN' + sysIds.join(',');
		} else {
			answer = 'sys_idINempty';
		}

		return answer;

    },

    type: 'PhoneSystemClassQualifier'
};

 

I also ran the function's content in a background script and gs.info() printed the expected class labels. Unfortunately, Try It still shows all of the classes.

 

Oh, and I am afraid I don't know what the difference is between my referral qualifer and yours. Is it just a space between "javascript:" and "new"? Making that change didn't help.

Community Alums
Not applicable

Hi, try this instead.

 

Another solution though, why don’t you just use the simple reference qualifier e.g. class [IS] Kandy business solution. Etc to save yourself the tech debt?

//Reference qualifier on dictionary.
//javascript: new PhoneSystemClassQualifier().getPhoneSystemClasses(); 

 

var PhoneSystemClassQualifier = Class.create();
PhoneSystemClassQualifier.prototype = {
    initialize: function() {},

    getPhoneSystemClasses: function() {
        var classes = [
            'u_cmdb_ci_five9_phone_system',
            'u_cmdb_ci_kandy_business_solution_phone_system',
            'u_cmdb_ci_microsoft_teams_phone_system',
            'u_cmdb_ci_synoptek_efax'
        ];

        var gr = new GlideRecord('sys_db_object');
        gr.addQuery('name', classes);
        gr.query();

        var sysIds = [];
        while (gr.next()) {
            sysIds.push(gr.get('sys_id'));
        }

        if (sysIds.length == 0) { //removed the extra '='.
            return 'sys_idISEMPTY';
        } else {
            return 'sys_idIN' + sysIds.join(',');
        }
    },

    type: 'PhoneSystemClassQualifier'
};

 

 

 Please mark as helpful or if it’s resolved the issue, CORRECT!

Ankur Bawiskar
Tera Patron
Tera Patron

@Mike Hashemi 

Why not have a select box i.e. drop down with 4 choices?

if not then why not apply simple filter condition on that variable itself rather than script include and ref qualifier?

AnkurBawiskar_0-1735968735189.png

 

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