How to generate excel file from a script in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2022 08:10 AM
Hi
we need to generate Excel sheet a request to be placed in a file to be auto generated (possibly xls, xlsx etc. format) as an attachment & should be attached to the Requested item submitted.
We did had something similar to be done for an request item which was achieved by using the below snippet in the Runscript activity
All that is required is to retrieve the list of variables & then use the Write functionality of GlideSysAttachment API class.
i am trying this script pls help me on this :
var Headers = ["Number","Caller","Short Desc","Assignment Group", "Assigned To"];
var fileName = 'Incidents.xls';
var xlsData = ''; //The variable xlsData will contain a string which is used to build the xls file contents
for (var i = 0; i < Headers.length; i++) { //Build the Headers
xlsData = xlsData + '"' + Headers[i] + '"' + ',';
}
xlsData = xlsData+"\r\n";
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while(gr.next()) {
xlsData = xlsData + '"' + gr.number + '",' + '"' + gr.caller_id.getDisplayValue() + '",' + '"' + gr.short_description+'",' + '"' + gr.assignment_group.getDisplayValue() + '",' + '"' + gr.assigned_to.getDisplayValue() + '"';
xlsData = xlsData+"\r\n";
}
//attach the file to a record.
var grRec = new GlideRecord("incident");
grRec.addQuery("sys_id","00b4939bdb010110c8cf9026ca9619e7");
grRec.query();
if(grRec.next()){
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName,'application/xls',xlsData);
}
for example
This would then print data in Excel in format as above
If at all data is required to be printed in the format as above
with all Variable questions in Column A & its corresponding values in column B (something in tabular form) then all you need is to use the above snippet

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 03:39 AM
Seems fairly complicated with Script.
With Flow Designer, it takes 20 minutes 🙂
* Create a catalog item to order the file
* Process cat item in Flow Designer, using the "Utility Actions" spoke (available on store).
* Attach result to RITM.
Here's a recording.
If helpful or correct, please indicate so!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 04:08 AM
Hello Mattias Johnsson,
Thank you for your comment.
Now I'm trying to automatically export Configuration item records and added the result to catalog request but I don't know how to setting "Schema[File Schema]" in "Append to Delimited File".
Your comments is very helpful to me. Could you please also share the details that you setting in step "Append to Delimited File" and step "Get Attachments on Record"?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 02:01 AM
Certainly,
The action "Append to Delimited file" uses a schema defined.
Create the above Schema in IntegrationHub Utilities – File builder – Schemas
Docs: https://docs.servicenow.com/bundle/tokyo-application-development/page/administer/integrationhub-store-spokes/task/create-schema.html
If helpful or correct, please indicate so!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 03:51 AM
Hello Mattias Johnsson,
Thank you so much for your reply.
It's working and I can request a catalog to automatically export incident records, but the export file was't csv or excel file.
Also, I have checked your video that the export records was csv file. could you please give me some advice that where I can do this setting?
Thank you
Best regards
Chen