Sei sulla pagina 1di 4

Multi Row Variable Set

The following article describes how you can do certain things with Multi Row Variable Set (MRVS) that was introduced in
London version of ServiceNow. You will find articles on how to create a Variable set, we will take it a step further to:

1. How to access the Multi Row Variable Set.


2. How to access the variables in MRVS.

Use Case: Create a Catalog item that asks users to be added to the system.

Let’s say that you have already created your MRVS and its variables as follows:

Multi Row Variable Set Name: Insert User Info to Add


Internal Name: insert_user_info_to_add (This field might be hidden. The name is automatically given; that can also be
changed)
Variables:

Question Name Field Type


First Name u_firstname String
Last Name u_lastname String
Role to Add u_addrole Choice Value
Work Location u_worklocation Reference

Once the form is submitted, the above data is collected and stored as a JSON Object.
Let’s take a look at the data. Do the following:

Submit a request and get the sys_id of the RITM record


Open Scripts – Background
Type the following:

var mrvs;
var itemID = ‘c672bdae37f173000c20036973990e0e’; //RITM Record ID
var ritmGR = new GlideRecord('sc_req_item'); //Do the GR on the RITM Table
if (ritmGR.get(itemID)){
mrvs = ritmGR.variables.insert_user_info_to_add; //Internal name from above
}
gs.print(mrvs);

The data will be printed in the JSON format as follows: The following output shows two records.

*** Script: [ {
"u_firstname" : "Geroge",
"u_lastname" : "Clooney",
"u_addrole" : "u_developer",
"u_worklocation" : "25aba58c0a0a0bb3001b828703677dc6"
}, {
"u_firstname" : "Brad",
"u_lastname" : "Pitt",
"u_addrole" : "u_architect",
"u_worklocation" : "25aba4ba0a0a0bb3006ea3ebcfe1565b"
} ]

Notice two things above in the print out. The values of the two keys: Try to answer why.

u_role: “u_developer” AND “u_architect”


u_work_location: “af4a75e5dbd5bb482911e525ca961973” AND "25aba4ba0a0a0bb3006ea3ebcfe1565b"

Now let’s say you want to only print the individual values of each key. Do the following:

var mrvs;
var itemID = '04b0a383379133000c20036973990ee3'; //RITM Record ID
var ritmGR = new GlideRecord('sc_req_item'); //Do the GR on the RITM Table
if (ritmGR.get(itemID)){
mrvs = ritmGR.variables.insert_user_info_to_add; //Internal name from above
}
gs.print(mrvs.u_firstname);
gs.print(mrvs.u_lastname);
gs.print(mrvs.u_addrole);
gs.print(mrvs.u_worklocation);

The data will be printed in the JSON format as follows:

*** Script: [Geroge, Brad]


*** Script: [Clooney, Pitt]
*** Script: [u_developer, u_architect]
*** Script: [25aba58c0a0a0bb3001b828703677dc6, 25aba4ba0a0a0bb3006ea3ebcfe1565b]
So now we know how to access the MRVS and its variables. But the Role and the Work Location values are in an
unusable format, how can we get the Display Value?

Unfortunately,

gs.print(mrvs.u_worklocation.getDisplayValue()); doesn’t work. We have to do a Glide Record query. But before doing
the Glide Record Query, we need to get to:

1. The JSON Object


2. The Variable set and its variables.

I did the following in a Business Rule on the SC_Task table.

var itemID = current.request_item; // get the current RITM Number SYS_ID


var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sys_id' , itemID);
ritmGR.query();
if (ritmGR.next()){
var mrvs = ritmGR.variables. insert_user_info_to_add; //Put JSON Object to variable
var jsonData = JSON.parse(mrvs); //Parse the JSON object
var output = 'FirstName' + ',' + 'LastName' + ',' + 'CompanyName' + ',' + 'EmployeeID' + ',' +
'EmailAddress' + ',' + 'PhoneNumber' + ',' + 'UserRole' + ',' + 'UsageLocation' + ',' + 'IntakeCountryValue' + '\n';
for (var i=0; i < jsonData.length; i++){ //Looping through each result within the JSON Object
var counter = jsonData[i];

//At this point I have all the Key Value pair data in the Variable called ‘counter’.
//Now we will get the actual readable value from the JSON Object’s Key Value Pair of Work Location and Roles

var getLocation = new GlideRecord('u_user_work_location');


getLocation.addQuery('sys_id', counter.u_worklocation.toString());
getLocation.query();
if (getLocation.next()){
var getLocationName = getLocation.name; //Inserted Display Value of the location in a variable
}

var getRole = new GlideRecord('question_choice');


getRole.addQuery('value', counter.u_addrole);
getRole.query();
if (getRole.next()){
var getRoleName = getRole.text; //Inserted Display Value of the Role in a variable
}

//Printing the results from above to the Description Field and a hidden field called Data(u_data)

output = output + (counter.u_first_name +','+ counter.u_last_name +','+ getRoleName +','+ getLocationName +'\n');
current.description = current.description + '\n' + output;
current.u_data = output;

}
}
})(current, previous);
I have done two things here.

1. Updating the Description Field


2. Created a field called Data(u_data) and updated that field with the result. I will use this field to download the
CSV of the results using an UI Action.

UI Action:

I created the following UI Action to download the CSV.

function exporttoCSV(){
var CSV = g_form.getValue('u_data');
//alert(CSV);
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
//this trick will generate a temp <a /> tag
var link = document.createElement("a");
var fileName = "Users_to_be_added";
link.href = uri;
//$scope.attach(link.download);
//set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv";

//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

I hope this helps.

Potrebbero piacerti anche