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

Thanks for this, I had the need to update the approval_variable_summary ui macro to add support for multi row variable sets, so your code snippet come in handy. Apparently it turned out that if you loop over more requested items this functions has problems, performance wise. It took the approval form over 10 seconds to open. So I modified the Script to perform better, mostly by the use of the API for Multi row variable sets and avoiding unnecessary database queries.

This is the code of the global Script Include. Note, this has to be in global scope to work but you can call MRVSUtil.print from scoped applications.

var MRVSUtil = function () {
  /*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. */
  var printMRVS = function (current, varSetName) {

    var output = [];
    var gr = current;
    var mrvs = gr.variables[varSetName];
    var rowCount = mrvs.getRowCount();

    if (mrvs === undefined || !current || !rowCount) {
      return output;
    }

    //Get the title of the multi row variable set. 
    var title = current.variables[varSetName].getLabel();
    output.push(title + ': '); //Add the title to the output
    output.push("- - - - - - - - - - -");
    output = output.concat(current.variables[varSetName].getRows().reduce(function (result, row) {
      return row.getCells().reduce(function (result, cell) {
        var cellLabel = cell.getLabel();
        var cellDisplayValue = cell.getCellDisplayValue();
        if (cellLabel && cellDisplayValue) result.push(cellLabel + ": " + cellDisplayValue);
        return result;
      }, result);
    }, []));
    output.push("- - - - - - - - - - -");
    return output;
  };
  
  return {
    print: printMRVS
  };
};

Nice! I wasn't aware of 'getCellDisplayValue' when I wrote that. I posted a question in another thread about getting the display value of reference fields in mrvs and at the time I didn't think it was possible. We also only used this for emails, and we only have 1 RITM to 1 REQ, but I think I'll update our script now. Thanks for posting. 🙂