Sending Attachment using Rest API

ramesh_r
Mega Sage

Hi

 

I want to send attachment using rest api for that i have created BR on the incident table and written the below script but its not working can anyone please correct this script

 

var target = new GlideRecord('sys_attachment');
target.addQuery('table_name', 'incident');
target.addQuery('table_sys_id', current.sys_id);
target.query();

while(target.next()) {
var t = target.content_type;
var sa = new GlideSysAttachment();
var binData = sa.getBytes(target);
var base64Data = GlideStringUtil.base64Encode(binData);

var s ="https://dev61883.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=82c3b785dbd7a300a8890d53ca9619ac&file_name="+target.file_name;

var body= {
"name":t,
"source":"incident:38a83c61db132300a8890d53ca96194d",
"payload":base64Data
};
//Send Attachments
var requestAttachment = new sn_ws.RESTMessageV2();
requestAttachment.setEndpoint(s);
requestAttachment.setHttpMethod('POST');
requestAttachment.setBasicAuth('admin','adminadmin');
requestAttachment.setRequestHeader("Accept","application/json, text/plain, */*");
requestAttachment.setRequestHeader('Content-Type','application/json');
requestAttachment.setRequestBody(body);
var response = requestAttachment.execute();
gs.addErrorMessage(response.getBody());

}

 

1 ACCEPTED SOLUTION

josh_nerius
ServiceNow Employee
ServiceNow Employee

Hi Ramesh, 

The Attachment API at /now/attachment/file expects a binary payload. Base64 won't work in this case. 

There's an easy fix. RESTMessageV2 has a method called setRequestBodyFromAttachment(attachmentSysId). Use this instead of setRequestBody. You can remove all of the code that calls GlideSysAttachment and just pass the Sys ID of the attachment to this method. 

View solution in original post

14 REPLIES 14

@josh_nerius  Does this function setRequestBodyFromAttachment(attachmentSysId) send the attachment in Base64 format? If not, how to convert into base 64 format and send through rest message?

Manjunath7
Kilo Explorer

 

Can you  please help me. I am trying write a script . which reads all the incidents and if it  has attachment check if it is excel file and then if it is only excel file then scan the contents of it for particular excel file for  keyword like word "testing" . if it is there then print "found" and then print Ticket number. Is this possible..?

Jatin4
Kilo Explorer

Which business rule has been used here?

Aoife Lucas
Giga Expert

I would never build a REST message from scratch like that.  I would setup a REST Outbound Message and use it instead.  You can use variables for the header, the URI, and the body so it makes it more reusable.

Aoife Lucas

shashankverma05
Tera Contributor
Refer the below code this will work fine on attachment table or in BG    
 
var getAttachmentMeta = new sn_ws.RESTMessageV2();
    getAttachmentMeta.setEndpoint('https://xxxinstancenamexxx.service-now.com/api/now/attachment?sysparm_query=table_name%3Dincident%5E...'+current.table_sys_id+'&sysparm_limit=1');
    getAttachmentMeta.setBasicAuth('admin', 'xxxxxxx');
    getAttachmentMeta.setHttpMethod('GET');
    getAttachmentMeta.setRequestHeader("Accept", "application/json");

 

    var metaResponse = getAttachmentMeta.execute();
    var metaResponseBody = JSON.parse(metaResponse.getBody());

 

        var attachmentSysId = current.sys_id.toString();
        var fileName = current.file_name.toString();

 

        var sendFileRequest = new sn_ws.RESTMessageV2();
        sendFileRequest.setEndpoint('https://xxxinstancenamexxx.service-now.com/api/now/attachment/file');            
        sendFileRequest.setBasicAuth('admin', 'xxxxx');
        sendFileRequest.setHttpMethod('POST');
        sendFileRequest.setRequestHeader("Accept", "application/json");
        sendFileRequest.setRequestHeader("Content-type", "multipart/form-data");
        sendFileRequest.setQueryParameter('table_name', 'incident');
        sendFileRequest.setQueryParameter('table_sys_id', 'SYS_ID'); /*Pass the sysid of third party incident.*/
        sendFileRequest.setQueryParameter('file_name', fileName);
        sendFileRequest.setRequestBodyFromAttachment(attachmentSysId);
        var sendResponse = sendFileRequest.execute();

 

})(current, previous);