Catalog Variable Export in CSV

Sandeep Bharate
Tera Contributor

Hi All,

I trying to implement a UI action which allows us to export Requested Item number, item name, description and its variables (questions and values) in CSV file.

How to achieve this?

OOB export only export requested item(current record) but not its variables.

 

Thanks

Sandeep Bharate 

1 ACCEPTED SOLUTION

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi Sandeep,

please try to create your UI Action using the below script (change <field1>, <field2>, <field3> with your own fields name):

var output = "Number,Company,Short_Description"; //header for csv file
var table = "sc_catalog";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);

var count = 0;

while (gr.next()) {
       count++;
       output += "\n" + gr.<field1> + "," + gr.<field2>.getDisplayValue() + "," + gr.<field3>;
       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);
}

When the UI Action will run, you can navigate to sys_attachment.list to download export.csv file.

Hope this will be useful for you.

Please, mark Correct or hit Helpful if you find my response worthy.

Cheers
Alberto

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sandeep,

you can create report for this and export that.

use report directly on sc_req_item table

https://docs.servicenow.com/bundle/london-performance-analytics-and-reporting/page/use/reporting/task/use-service-catalog-variables-in-report.html

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi Sandeep,

please try to create your UI Action using the below script (change <field1>, <field2>, <field3> with your own fields name):

var output = "Number,Company,Short_Description"; //header for csv file
var table = "sc_catalog";
var recordId = ""; //using for attachment file
var gr = new GlideRecord(table);

var count = 0;

while (gr.next()) {
       count++;
       output += "\n" + gr.<field1> + "," + gr.<field2>.getDisplayValue() + "," + gr.<field3>;
       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);
}

When the UI Action will run, you can navigate to sys_attachment.list to download export.csv file.

Hope this will be useful for you.

Please, mark Correct or hit Helpful if you find my response worthy.

Cheers
Alberto

Hi Sandeep,

Any update on this?

Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread.

Thanks in advance.

Cheers
Alberto

@Alberto Consonni 

hi,

I want to download the file right away instead of attaching it to a record, is it possible?

can you please provide the code to be used in UI actions. 

thanks