- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2019 01:06 AM
Hello Community.
I am very new to ServiceNow and currently am trying to create a service catalog to order company uniform and accessaries.
The below is the sample Try-it page.
When ordered, the variable sets are displayed like the image below in the Requested Item page.
I would like to write these variable set values into a csv file to be exported.
And this is the tricky part.
I need to make the csv format somewhat like the image below (This is the order placement format of our uniform supplier company, and we cannot change it...).
"Item" comes first as the key, and writing into csv is done not per Request Item, but regularly (twice a day, morning and afternoon).
Is it possible to extract variable set values from multiple Request Items into the same csv file?
And my boss says he wants to do this without creating a new table to store these variable set values.
If possible, what is the simplest way to achieve it?
I appreciate your help.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2019 07:58 PM
Step1 : extract the values from your ritm
step2 : pass the values into file creator.
Code to fetch variable set values from RITM:
var mrvs;
var itemID = '70506da8db002300e69dfbef2996194a';
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(itemID)) {
mrvs = ritmGR.variables.access_list;
}
gs.print(mrvs);
code to create a CSV file from backgrount script:
var output = "Number,Company,Short_Description"; //header for csv file
var table = "incident";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);
gr.addEncodedQuery("caller_id=javascript:gs.getUserID()^active=true"); //Incident was assigned to Beth Anglin
gr.query();
var count = 0;
while (gr.next()) {
count++;
output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description;
if (!recordId) {
recordId = gr.sys_id;
}
}
gs.print(recordId);
writeAttachmentFile(output);
function writeAttachmentFile(data) {
var attachment = new Attachment();
var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);
}
you need to apply few changes to merge thses to get the desired output.also it is possible to get many RITMs value and insert into the same file but the only blocker is it will be attached in a single record as you configured.
-satheesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2019 07:58 PM
Step1 : extract the values from your ritm
step2 : pass the values into file creator.
Code to fetch variable set values from RITM:
var mrvs;
var itemID = '70506da8db002300e69dfbef2996194a';
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(itemID)) {
mrvs = ritmGR.variables.access_list;
}
gs.print(mrvs);
code to create a CSV file from backgrount script:
var output = "Number,Company,Short_Description"; //header for csv file
var table = "incident";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);
gr.addEncodedQuery("caller_id=javascript:gs.getUserID()^active=true"); //Incident was assigned to Beth Anglin
gr.query();
var count = 0;
while (gr.next()) {
count++;
output += "\n" + gr.number + "," + gr.company.getDisplayValue() + "," + gr.short_description;
if (!recordId) {
recordId = gr.sys_id;
}
}
gs.print(recordId);
writeAttachmentFile(output);
function writeAttachmentFile(data) {
var attachment = new Attachment();
var attachmentRec = attachment.write(table, recordId, "export.csv", "text/csv", data);
}
you need to apply few changes to merge thses to get the desired output.also it is possible to get many RITMs value and insert into the same file but the only blocker is it will be attached in a single record as you configured.
-satheesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2019 05:28 PM
Thank you so much, Satheesh!
Sorry for a stupid question...
Do I suppose to write the code you suggested in "Run script" in a linked workflow?
Thank you in advance.
Lit

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2019 06:28 PM
yes you can add the code in workflow run script block to generate a file and attach in the same ritm record.
Pass referencing sys ids accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2019 05:32 PM
Thank you, Sateesh!
Sorry for another stupid question.
This is about "ItemID" this time.
Is it suppose to be the sys ID of a variable set?
I will give some tries while i wait for your reply, and i'll let you know if I found out in the meantime.
Cheers!