How I can Access Variable set from workflows ( Multi rows )

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2019 07:56 AM
Hello Everyone,
I have a requested item thats called " A " , once this CI is done, I will create a requestd item " B "!
My code like this:
var cart = new Cart(null, gs.getUserID()); //Create the cart as the current user
var item = cart.addItem("439be702db99845007e69334ca961968");
cart.setVariable(item, 'title', 'ServiceNow Testing');
everything is find until now 🙂 , the problem is , I have a Multi Rows Variable set, I need to fill the Multi Rows Variable set ?how i can access to the Multi Rows Variables ?
NOTE : I have Tried this code(does not working)
variable_pool.MyVar
NOTE : I need to access Multi Rows Variable set thats exist in Requested item B and my workflow(include the code) based on A
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2019 09:12 AM
Hi Saif -
The values inside of the MRVS are not directly accessible through the single var method of current.variables.variable name.
You can access the entire MRVS in JSON using the current.variables.MRVS_NAME syntax.
For each element, you can use current.variables.MRVS_NAME.variable_name and get an array of each row's value for that question.
The article below shares the methods available for MRVS:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2019 08:56 AM
Saif,
Keep in mind that if there is an empty value in one of you mrvs fields, the key/value pair in the JSON object won't show up at all when you output the JSON in your script. If you try to output a value that doesn't exist in you mrvs, your script won't run! Since you know how many mrvs fields there are, you can iterate through the JSON fields based on this amount and check that the field exists, or in other words, check that your user entered a value.
The below script is grabbing a RITM (one which I know has mrvs values populated) with a hard coded sys_id.
Grab the mrvs with its internal name: ritm.variables['internal_name']. The internal name of my mrvs is 'demo_mrvs'. Once you grab the mrvs, it will return an array of JSON objects. Use the JSON.parse() function so you can use indexes to access each mrvs row.
See below code for context...
var valueArr = []; //will hold mrvs values
var valueArrays = []; //will hold array of values
var current = new GlideRecord('sc_req_item');
current.get('797d30addb250010b94b58b3ca961978');
if (current.next()) {
var m = current.variables['demo_mrvs'];
var mrvParsed = JSON.parse(current.variables['demo_mrvs']); //parsing mrvs
var setCount = mrvParsed.length; //# of obj's in mrvs array - need for obj iteration
//first object's keys - only return keys with value, may come up short if you don't
//check for values
var keys = Object.keys(mrvParsed[0]) + '';
keysArr = keys.split(','); //array of keys need for 'key' iteration
//iterate through obj
for (var i = 0; i < setCount; i++) {
//iterate through keys
for (var j = 0; j < keysArr.length; j++) {
//check to see if mrvs value exists, if not, hard code string value
if (keysArr[j]) {
valueArr.push(mrvParsed[i][keysArr[j]]);
//if no value entered...
} else {
valueArr.push("no data entered");
}
}
valueArrays.push(valueArr);
valueArr = []; //empty array to repopulate with next row of mrvs values
}
}
return valueArrays;
In this particular example, I'm returning a 2d array of the values. Each internal array signifies a mrvs row. You may need to mold this code for your purposes but this is how you access each mrvs row and in turn, each mrvs value. You can put this code in a background script and replace my ritm sys_id and mrvs internal name with your own values.
Hope this helped!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2019 06:23 AM
Hello Saif,
Refer to this article where i have described how to populate a MRVS.
Mark the comment as a correct answer and also helpful if this helps.