Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
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:
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:
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.
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);
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:
//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
//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.
UI Action:
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);
}