The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Deepak Ingale1
Mega Sage

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

 

find_real_file.png

find_real_file.png

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'
};