How to extract Service Catalog variable set values and output as a csv file?

L_10
Kilo Contributor

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.

find_real_file.png

When ordered, the variable sets are displayed like the image below in the Requested Item page.

find_real_file.png
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).

find_real_file.png


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.

1 ACCEPTED SOLUTION

SatheeshKumar
Kilo Sage

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

View solution in original post

8 REPLIES 8

SatheeshKumar
Kilo Sage

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

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

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.

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!