Contents not being rendered when sending attachment (doc,docx,xls) using outbound REST API

jeet_bhatt
Kilo Explorer

Hi guys,

I'm trying to send an attachment from ServiceNow to a third party app called HP TRIM (record management system) using POST API REST call. I'm using 'multipart/form-data' as content-type in request header. Rtf, txt files are successfully getting created as records and contents are properly rendered as per actual document but when it comes to doc, docx, xls or pdf files, the records are getting created but content in the records are not being rendered correctly. As a result, I can't preview document in HP TRIM. There's no settings issue in TRIM. When I try sending docx,xls, pdf files using POSTMAN app, the contents are being correctly rendered.

Any ideas what could be the reason?

1) Below is REST message (POST):

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="RecordContainer"

${studentid}

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="RecordRecordType"

${recordtype}

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="RecordTitle"

${recordtitle}

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="Files"; filename="${filename}"

Content-Type: ${contenttype}

${content}

------WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="properties"

RecordMimeType

------WebKitFormBoundary7MA4YWxkTrZu0gW--

2) Below is code which sets parameters in the REST msg:

prepareSendToRM8:function(processQue) {

var grAtt = new GlideRecord('sys_attachment');

if (grAtt.get(processQue.u_payload)) {

        var StringUtil = new GlideStringUtil();

        var contentStr = '';

        var gsa = GlideSysAttachmentInputStream(grAtt.sys_id.toString());

        var bytesContent = new Packages.java.io.ByteArrayOutputStream();

        gsa.writeTo(bytesContent);

        bytesContent.close();

        var mycontentStr = new Packages.java.lang.String(bytesContent.toByteArray());

        contentStr = mycontentStr;//StringUtil.base64Encode(mycontentStr);

        var recordTitleStr = escape(String(processQue.u_description));

        studentUriStr = this.getStudentID(processQue.u_source_table, processQue.u_source_record);

        //Get filename from Attachment record

                  fileName = grAtt.file_name;

        //Get Content type of attachment

                  var contentType = '';

                  contentType = grAtt.content_type;

        if (studentUriStr != '') {

                  var responseStr = this.sendDocumentRM8(contentStr, studentUriStr, recordTitleStr, fileName, contentType);

                  var responseObj = JSON.parse(responseStr);

        }

}

3) Below is code which makes Outbound REST API call:

sendDocumentRM8:function(contentStr, studentUriStr, recordTitleStr, fileName, contentType) {

        var responseObj = {};

        responseObj.responseBody = '';

        var docMsg;

        try {

                  docMsg = new sn_ws.RESTMessageV2('RM8TRIM', 'post');

                  docMsg.setStringParameterNoEscape('content', contentStr);

                  docMsg.setStringParameter('studentid', studentUriStr);

                  docMsg.setStringParameter('recordtitle', recordTitleStr);

                  docMsg.setStringParameter('recordtype', 'Document');

                  docMsg.setStringParameter('filename', fileName);

                  docMsg.setStringParameter('contenttype', contentType);

                  docMsg.setHttpTimeout(10000); // Wait for response

                  var response = docMsg.execute(); // Might throw exception

                  responseObj.responseBody = response.getBody();

                  responseObj.responseState = response.haveError() ? 'Error' : 'Processed';

                  responseObj.status = response.getStatusCode();

            }

        catch(ex) {

                  responseObj.responseBody = response.getErrorMessage();

                  responseObj.responseState = 'Error';

                  responseObj.status = '502';

          }

        finally {

                  responseObj.requestBody = docMsg ? docMsg.getRequestBody():null;

          }

          return JSON.stringify(responseObj);

},

1 REPLY 1

jarodm
Mega Guru

Are you still looking for help on this item? Do you have a solution?