Attached excel file is corrupted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 04:12 AM
Hi Team,
I am trying to attach some API responses as an Excel file to some records in ServiceNow. The file is getting attached to the record, but when I am trying to open it, it shows one error message as below:-
Please check the below mentioned code I have written and help me to resolve this issue.
var fileResponse = this.utils.makeApiCall("GET", "/reporting/exports/" + exports_id + "/download", "", this.getAuthenticationObject());
if (fileResponse && (fileResponse.getStatusCode() == "200" || fileResponse.getStatusCode() == "201")) {
var fileContent = fileResponse.getBody(); // Get the response body directly
var attachment = new GlideSysAttachment();
// var agr = attachment.write(pdf,fileName, 'text/csv', fileContent);
//var agr = attachment.write(pdf, fileName, 'application/vnd.ms-excel', fileContent);
var agr = attachment.write(pdf, fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', fileContent);
}
Thanks in advance!
Please help me to resolve this issue ASAP, it's urgent.
@Chuck Tomasi , @umaaggarwal , @Ankur Bawiskar , @Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 04:39 AM
Hi,
Please check if the filecontent you are getting is in base64 format.
To check that you can use below script
var fileContent = fileResponse.getBody();
isValid=GlideStringUtil.isBase64(fileContent);
gs.info(isValid);
if this returns false then you need to convert the data into base64 format and add as attachment
var base64String = GlideStringUtil.base64Encode(fileContent)
var fileName = 'example.txt';
var contentType = 'text/csv';
var agr = attachment.writeBase64(pdf, fileName, 'application/vnd.ms-excel', base64String);
Please mark it helpful if it works
Thanks & Regards
Sejal Chavan