Dynamically finding and looping through Multi row variabl sets (MRVS)

Community Alums
Not applicable

Afternoon All,

Looking for some help, I've tried multiple things and I'm pulling my hair out. I have a request form with multiple MRVS on it. I need to loop through current.variables to find the MRVS, then loop through the MRVS to get the keys to verify the quantity field is not empty or 0. If not I need to return that entire row to description.

 

I've tried a few things but my brain has finally given up and melted so I can't think straight, below was my last attempt just to get the looping right:

var test2 = 'MRVS Variables:\n';
var mrvs_list = [];
var mrvs_vars;

//This loops through current.variables to get the MRVS names
for (var i in current.variables) {
    if (i.toString().startsWith('u_') == true) {
        test2 += i.toString() + '\n';
        mrvs_list.push(i);
    }
}

//
test2 += '\n\n mrvs_list: ' + mrvs_list.toString() + '\n';

//looping through the list of mrvs's pushed to mrvs_list array
for (var i2 in mrvs_list) {
    test2 += '\n\n Current MRVS: ' + mrvs_list[i2].toString(); + '\n';
	//attemptint to loop through all the keys for that mrvs
    for (var i3 in eval('current.variables.' + mrvs_list[i2])) {
       test2 += 'Current Var: ' + i3.toString() + '\n';
    }
}


 task.work_notes = test2;

 

1 REPLY 1

dbasi
Tera Contributor

Hi Simon,

 

I've just posted an article about looping through MVS in a flow (still being moderated, will post it here after it's available, https://community.servicenow.com/community?id=community_article&sys_id=f049e590dbb554904819fb2439961...).

Below is my quick attempt at doing what your asking: 

var catItem = new GlideRecord('sc_cat_item'); // need to GR into the catalog item to pull the sys_id
catItem.get(current.cat_item);

var variableSets = new GlideRecord('io_set_item');
variableSet.addEncodedQuery('sc_cat_item.sys_id=' + catItem.sys_id + '^variable_set.type=one_to_many'); // query on the variable set table where the cat item is sys id and the type of variable is multi set
variableSet.query();

var setsNameArray = []; // new array to store internal names of the MVS's

while (variableSet.next()) {

    setsNameArray.push(variableSet.variable_set.internal_name); // push internal names for calling on RITM

}

var checkQuantity = false; //boolean to check the quantity is != 0
var rowDescription = []; // to store the variable row objects

for (var j = 0; j < setsNameArray.length; j++) { // loop through internal names
    for (var i = 0; i < current.variables.setsNameArray[j].getRowCount(); i++) { // loop through number of rows end user created per MVS
        var currentset = setsNameArray[j]; // stores current object/row of MVS
        if (current.variables.currentSet[i].quantity != 0) { // checks quantity where internal name of the quantity variable is "quantity", if it is unknown you'll need to GR into the variable set and story the internal name of quantity.
            checkQuantity = true; // sets boolean true as it's found one where quantity is != 0;
            rowDescription.push(current.variables.currentSet[i]); // pushes the current row/object into array
        }
    }
}
    var test2 = 'MRVS Variables:\n';

    if (checkQuantity) {
        for (var k = 0; k < rowDescription.length; k++) {
            test2 += '\n\n Current MRVS: ' + rowDescription[k]; // process data using stored objects
        }
    }

task.work_notes = test2;

/* This should output the data like this:
{
  "application_implementation_language" : "2",
  "network_zone" : "1",
  "component_name" : "dasda",
  "host_operating_name" : "1",
  "data_center" : "e77e1a4d0f4ed600717bb18ce1050ee2",
  "life_cycle_stage" : "2",
  "brand" : "1",
  "host_type" : "2",
  "application_server" : "1",
  "host_name" : "dasdasd"
}
*** Script: {
  "application_implementation_language" : "2",
  "network_zone" : "1",
  "component_name" : "dasda",
  "host_operating_name" : "1",
  "data_center" : "e77e1a4d0f4ed600717bb18ce1050ee2",
  "life_cycle_stage" : "2",
  "brand" : "1",
  "host_type" : "2",
  "application_server" : "1",
  "host_name" : "dasdasd"
}

As it's in object format it may not like this output and you may need to process it as such
*/

 

There may be errors in the code as I don't have my instance setup to test this.

 

But at it's core you can query the variable set table for the catalog item and MVS type, pull the internal names and then check if the variables on the RITM have quantites != 0. This will work if the quantity internal name is constant (i.e. "quantity") if that is unknown you may need to figure out a way of pulling that variables internal name for each MVS.

 

Cheers,

Dal