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

Mehta
Kilo Sage
Kilo Sage

Do you want to set the request body as binary or there will be a json format that you need to sent as request body to respective api?

This is the request api body:
{
"request":
{
"comment": {"body": "123",
"uploads":["token "]
}
}
}

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

Thank you for the solution. I am able to send Text attachments from ServiceNow to Zendesk. However when I am trying to attach PDF/PNG or any other format with Content-Type as multipart/form-data. It is not working.