Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attached excel file is corrupted

Community Alums
Not applicable

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

 

chaithra4_0-1724065845823.png

 

 

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 

 

 

1 REPLY 1

sejal1107
Tera Guru
Tera Guru

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

Please check and Mark Helpful and Correct if it really helped you.
Thanks & Regards
Sejal Chavan