logo

NJP

Variable Information of Catalog Item in Question and Value format.

Import · Dec 22, 2018 · article

Hello,

https://hi.service-now.com/kb\_view.do?sysparm\_article=KB0714632.

https://community.servicenow.com/community?id=community\_question&sys\_id=8fc10749db2aef806c1c02d5ca9619fb

There is an issue reported to get label of variable using getGlideObject API, so I have created below utility which gives catalog item variable information in Quenstion and Value format

image

image

var UCatalogVariableInfo = Class.create();
UCatalogVariableInfo.prototype = {
    initialize: function() {
        this.excludedType = ['12','19','20','24','11','14','15','17','23','25'];
        this.aru = new ArrayUtil();
    },

    getVariableInformation : function (grRec) {
        var variableNameLabel = {};
        var variableInfo = {};

        variableNameLabel = this.getVariableNameLablePair(grRec);

        for ( var v in variableNameLabel) {
            variableInfo[variableNameLabel[v]] = grRec.variables[v].getDisplayValue();
        }

        return variableInfo;
    },

    getVariableNameLablePair : function (grRec) {
        var variableHash =  this.getVariables(grRec);
        return variableHash;
    },

    getVariables : function (grRec) {
    var lookupHash = {};
        var variableSetSWithItem = this.getVariableSetsForItem(grRec);

        var gr = new GlideRecord("item_option_new");
        gr.addQuery("cat_item", grRec.getValue("cat_item")).
            addOrCondition("variable_set", "IN", variableSetSWithItem);
        gr.query();

        while (gr.next()) {
            if ( this.aru.indexOf(this.excludedType, gr.getValue("type")) == -1 ) {
                lookupHash[gr.getValue("name")] = gr.getValue("question_text");
            }
        }

        return lookupHash;

    },

    getVariableSetsForItem : function (grRec) {

        var set = [];

        var setItem = new GlideRecord("io_set_item");
        setItem.addQuery("sc_cat_item", grRec.getValue("cat_item"));
        setItem.query();

        while (setItem.next()) {
            set.push(setItem.getValue("variable_set"));
        }

        return set;
    },

    type: 'UCatalogVariableInfo'
};
View original source

https://www.servicenow.com/community/itsm-blog/variable-information-of-catalog-item-in-question-and-value/ba-p/2268566