- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2019 02:48 AM
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());
}
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2019 07:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 03:22 AM
@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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2019 02:52 PM
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..?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2020 11:02 PM
Which business rule has been used here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2020 11:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2024 03:49 AM