Attach an Excel to a record

SwarnadeepNandy
Mega Sage

I have an URL, which gives me an Excel (assume a downloadable link). I want that excel to get attached to a record in ServiceNow. We can get the such kind of link from ServiceNow's Excel web services as well. How can we achieve that. I tried something like below.

var request = new sn_ws.RESTMessageV2();
var instanceUrl = 'https://<tesinstance>.service-now.com/';
request.setEndpoint(instanceUrl + 'incident_list.do?CSV');
request.setHttpMethod('GET');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'userName';
var password = 'password';

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");//Tried changing the Accept encoding also

var response = request.execute();
var binaryFile = response.getBody();//Receiving excel file, binary data.

var attachment = new GlideSysAttachment();

//Tried both the below lines

// attachment.writeBase64(customerImpactedGr, "Incident list.xls", "application/vnd.ms-excel", gs.base64Encode(binaryFile));
attachment.writeBase64(customerImpactedGr, "Incident list.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", gs.base64Encode(binaryFile));

Some file is getting attached, but when i tried opening the file, it seems that it is a corrupt file. Whereas this works fine with CSV file.

I tried one more thing. I converted an excel file using window.btoa() api using client side javascript and the base64 encoded file which i receive, works perfectly fine. So what i am suspecting is that gs.base64Encode() api not working correctly with binary data. Or i maynot be handling the binary data correctly.

If any one did something similar, your help will be highly appreciated.

Thanks & Regards,

Swarnadeep Nandy

1 ACCEPTED SOLUTION

SwarnadeepNandy
Mega Sage

I found a solution to the issue.

There is an API saveResponseBodyAsAttachment() OOTB. Which directly attaches the file in a record in ServiceNow.

Irrespective of file type being received, this API will take care of everything.

Regards,

Swarnadeep Nandy

View solution in original post

5 REPLIES 5

SatheeshKumar
Kilo Sage

can you try 

StringUtil.base64DecodeAsBytes(binaryfile);

if the above doesn't helped try to use the below.

var eccGr = new GlideRecord('ecc_queue');
eccGr.initialize();
eccGr.setValue('agent', 'AttachmentCreator');
eccGr.setValue('topic', 'AttachmentCreator');
eccGr.setValue('name', fileName+':'+contentType);// modify 
eccGr.setValue('source', tableName+':'+tableSysId);// modify
eccGr.setValue('payload', binaryfile);
var sys_id = eccGr.insert();

-satheesh

SatheeshKumar
Kilo Sage

the syntax is :

writeBase64( GlideRecord gr, String fileName, String contentType, String content_base64Encoded)

 

are you passing a valid record object?  as per your script customerImpactedGr  should be object of your record in which the attchment needs to be added. can you verify it?

Hi Satish,

'customerImpactedGr' consider it as any GlideRecord Object, where i need to attach the record. There were lines of new GlideRecord, which i thought would be redundant.

The problem is with base64 encoding of the excel file. If that happens correctly, i can use any API to attach.

Regards,

Swarnadeep Nandy

SwarnadeepNandy
Mega Sage

I found a solution to the issue.

There is an API saveResponseBodyAsAttachment() OOTB. Which directly attaches the file in a record in ServiceNow.

Irrespective of file type being received, this API will take care of everything.

Regards,

Swarnadeep Nandy