- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 05:55 AM
Hi All,
I am creating an excel and attaching to a record.
I am using the below code to attach the file:
var grRec = new GlideRecord("sc_task");
grRec.addQuery("sys_id", current.sys_id + "");
grRec.query();
if (grRec.next()) {
var grAttachment = new GlideSysAttachment();
grAttachment.write(grRec, fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', csvData);
}
It works perfectly for application/csv but not for xlsx format and I need the .xlsx format. The file gets created and attached but when I try to open it, it says the file is corrupted.
Could anyone tell me what am i doing wrong here, please?
Thanks!!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 06:51 AM
Hi,
it's not straight forward for xlsx file and it won't work
Either keep it csv or else you would require custom solution to create xlsx file and attach to record
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 04:14 PM
The problem is on using .write() instead of .writeBase64() or .writeContentStream(). .write() will just create a text file instead of a binary file. That's probably the reason why csv is working and not xlsx.
That said, where is the excel file coming from?
If it's being uploaded by a REST POST, it would be easier to use RESTMessageV2 .saveResponseBodyAsAttachment() method. There's an example in the documentation.
Following script will copy an xslx file that's attached to a requested item to an incident.
var grRec = new GlideRecord("incident");
if (grRec.get('<sys_id of an incident to attach xlsx file>')) {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_req_item');
gr.addQuery('table_sys_id', '<sys_id of ritm to copy xlsx from>');
gr.query();
if (gr.next()) {
var fileName = gr.getValue('file_name');
var contentType = gr.getValue('content_type');
var sys_id = gr.getValue('sys_id');
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
grRec,
fileName,
contentType,
gsa.getContentStream(sys_id));
}
}
Execution result. File test1.xlsx is attached to an incident.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 08:17 PM
Hey,
I am creating the csv file through a script (Business Rule).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 09:28 PM
Just making file extension "xlsx" and setting the content type isn't going to create an Excel file. That's probably why the file was corrupt. If the file extension of the downloaded file is changed to .txt, it probably would have opened.
xlsx file may contain images so it has to be processed as binary file.
Need to use export excel uri or install third party javascript excel utility in script include to generate xlsx file on server.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 09:54 PM
okay Hitoshi, Thank you for your help!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 05:11 PM
If the task is to upload an excel file from an external server to ServiceNow, I've provided a PowerShell script in the following thread.
FYI,
I've been able to attach an xlsx sheet to ServiceNow and been able to download and open the file in ways I've mentioned.