Not able to open attached excel file to a record (xlsx format)

Tanwir Singh11
Tera Expert

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!!!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

View solution in original post

9 REPLIES 9

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/sn_ws-namespace/c_RESTMessageV2A...

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.

find_real_file.png

Hey,

I am creating the csv file through a script (Business Rule).

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.

okay Hitoshi, Thank you for your help!!

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

https://community.servicenow.com/community?id=community_question&sys_id=5e264251db194d10bb4a474d1396...

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.