Multi-row variable set questions and values

Rick Mann
Tera Expert

I'm working with the new Multi-row variable set in the London release and doing some workflow scripting to create a task description that contains all of the rows from the variable set.  I have a script that works for what I'm trying to accomplish, but I think may be inefficient.  Is there a way to grab a list of questions from the variable set?  I'm also able to parse the JSON response to populate the description, but this gives me the variable name and value.

Thanks for any help on improving my script.

find_real_file.png

find_real_file.png

var sid = current.sys_id;

var gr = new GlideRecord('sc_req_item');
if(gr.get(sid)){
	var variables = gr.variables.getElements();
	var rowcount = gr.variables.dns_request.getRowCount();
	for (var i=0;i<variables.length;i++){
		var question = variables[i].getQuestion();
		var desc1 = '';
		desc1 += question.getLabel() + ': ' + question.getDisplayValue();
	}
	
	var desc2 = '\nNumber of DNS entries requested: ' + gr.variables.dns_request.getRowCount() + '\nEntry details:\n\n';
	
	var desc = '';
	for(var rc=0;rc<rowcount;rc++){
		var row = gr.variables.dns_request.getRow(rc);
		desc += 'Preferred name: ' + row.preferred_dns_name + '\nEntry type: ' + row.entry_type + '\nIP Address: ' + row.ip_address + '\nZone: ' + row.zone + '\n----------------\n';		
	}
	current.description = desc1 + desc2 + desc;
}
6 REPLIES 6

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Off the top of my head, you shouldn't need to do the GlideRecord.get as current already represents the record. I'm not quite sure what the ask is here, are you looking to make it all more dynamic? I think all of the new methods that came out with the multi-row variable set are related to the values, so you might have to gliderecord query the variable set variable definitions to grab the questions.

Rick Mann
Tera Expert

Thanks for the tip on using current instead of the get.  At this point, I'm really looking to just get a list of the Questions within the variable set in order to present a more readable description for the technician.

find_real_file.png

think you're going to have to query the variable definitions themselves to get a the question label, but I'll see if I can find something else.

Tim Grindlay
Kilo Sage

Hi,

Here's part of my solution. In addition to this we have the catalog table variable plugin so our scripts accommodate that, and I also needed to write a function that orders of the variables and variable sets and also excludes certain variables types or empties/false answers. Basically re-inventing the wheel on the GlideappVariablePoolQuestionSet() approach. The key for me was GlideappQuestion.getQuestion(). 

	/*formattedMRVS Returns an array of strings representing the multi row variable set

	current: Record holding the mrvs
	varSetName: The internal name of the multi-row variable set.

	Note: This assumes that the internal name of the mrvs is unique. */
	
	formattedMRVS : function(current, varSetName) {
		
		var output = [];
		var table = current.getTableName();
		
		var sys_id = current.getUniqueValue();
		
		var gr = new GlideRecord(table);
		gr.get(sys_id);
		var mrvs = gr.variables[varSetName];
		
		
		if (mrvs === undefined || !sys_id || !table || !mrvs.getRowCount()) {
			return output;
		}
		
		
		//Get the title of the multi row variable set. 
		var title = '';
		var varSet = new GlideRecord('item_option_new_set');
		varSet.addQuery('internal_name', varSetName);
		varSet.query();
		
		if (varSet.getRowCount() == 1 && varSet.next()) {
			title = varSet.title;
		} else
		title = 'Table';
		
		output.push(title + ' :'); //Add the title to the output
		
		//parse the multi row variable string
		
		rowCount = mrvs.getRowCount();
		var obj = JSON.parse(mrvs);
		
		for (var i=0 ; i < obj.length; i++) {
			
			var row = obj[i];
			
			output.push("- - - - - - - - - - -");
			for (var cell in row)
				{
				var cDisplay = this.getCellDisplay(varSetName, sys_id, cell, row[cell]);
				output.push(cDisplay);
			}
			
		}
		output.push("- - - - - - - - - - -");
		return output;
		
	},
	
	/*Gets the formatted output for the cell in the multi-row table variable set
	
	varSetName: Name of the variable/variable set name
	sys_id: sys_id of the record where the variable is
	questionName: name of the question/variable
	questionValue: value of the variable with which to get the display value.*/
	
	getCellDisplay : function(varSetName, sys_id, questionName, questionValue) {
		
		var questionId = "";
		
		//Query for the question sys_id
		var gr = new GlideRecord('sc_item_option_mtom');
		gr.addQuery('request_item', sys_id);
		gr.addQuery('sc_item_option.item_option_new.name', questionName);
		gr.addQuery('sc_item_option.item_option_new.variable_set.type', 'one_to_many');
		gr.addQuery('sc_item_option.item_option_new.variable_set.internal_name', varSetName);
		gr.query();
		if(gr.next())
			
		questionId = gr.sc_item_option.item_option_new.sys_id;
		
		
		//Get the question label and the display value of the input.
		var question = GlideappQuestion.getQuestion(questionId);
		if (question) {
			question.setValue(questionValue);
			return question.getLabel() + " : " + question.getDisplayValue();
		}
		
	},