I need to pass attachment into binary form from Servicenow to Zendesk.

Ahmer Faraz1
Tera Contributor

1. I created a Rest Message.
2. I created a Business Rule on the attachment Table.

3. I created one incident to add an attachment and trigger the business rule but the attachment pass in base 64 format only.


Business Rule-----------------------

var a = current.sys_id;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', a);
gr.query();
if (gr.next()) {
var StringUtil = new GlideStringUtil();
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gr);
encData = StringUtil.base64Encode(binData);
}
try {

attachmentRec = new GlideRecord("incident");
attachmentRec.addQuery("sys_id", current.table_sys_id);
attachmentRec.query();

while (attachmentRec.next()) {
ZendeskNumber = attachmentRec.u_zendesk_external_inc_number;

}

body.content_type = current.content_type + "";
body.u_zendesk_external_inc_number = ZendeskNumber + "";
body.number = attachmentRec.number + "";
body.file_name = current.file_name + "";
// body.base_64_bytes = binData + "";

body = JSON.stringify(body);

 

var r = new sn_ws.RESTMessageV2('', '');
r.setStringParameterNoEscape('file_name', current.file_name);

r.setRequestBody(body);

response = r.execute();
responseBody = response.getBody();
requestBody = r.getRequestBody();
httpStatus = response.getStatusCode();
jsonObject = JSON.parse(responseBody);

sysID = jsonObject.result.sys_id;

1 ACCEPTED SOLUTION

If you want to sent file as binary as a request body then you can use below code. It basically convert the file into binary and set the request body. But you wont be able to sent json or any other parameter in requested body. You might need to use query parameter or header to sent other details depending on the API document. 

 

var r = new sn_ws.RESTMessageV2('Rest Message', 'Upload');
            r.setRequestBodyFromAttachment(current.sys_id); //sys_id of attachment

View solution in original post

13 REPLIES 13

You do not need to set the content type as we are sending binary in body. You might have to reference third Party API Documentation for content type if needed, as it is different for each api/third party. 

 

 

Can you please mark my answer as helpful or correct, if it resolve your doubts 

Hello Mehta,

I am still not able to transfer pdf/word/excel format documents from ServiceNow to Zendesk. Only image and text files are transferred from the above method.

Can you please check documentation or try by not setting content type in header. Do check whether the tool can accept different content type apart from the one you have mentioned.  

Hi,

Any worked solution for this. i have same scenario. how did you implemented. 

Hello Ahmer,

 

any solution found