- 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
12-07-2022 09:05 AM
Does this function send the attachment in Base64 format?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2021 12:43 PM
Hi Josh,
Can you help me figure out why this is not working. This is my BR script
Table: Incident
When to run: After | Update
(function executeRule(current, previous /*null when async*/ ) {
var target = new GlideRecord('sys_attachment');
target.addQuery('table_name', 'incident');
target.addQuery('table_sys_id', current.sys_id);
target.query();
try {
gs.log("TEST Ebonding - Update");
var r = new sn_ws.RESTMessageV2('TEST Ebonding', 'Default PUT');
r.setStringParameterNoEscape('priority', current.priority);
r.setStringParameter('comments', current.comments.getJournalEntry(1).replace(/(\r\n|\n|\r|_123STREAMENTRY321_)/gm, ""));
r.setStringParameter('work_notes', current.work_notes.getJournalEntry(1).replace(/(\r\n|\n|\r|_123STREAMENTRY321_)/gm, ""));
r.setStringParameterNoEscape('category', current.category);
r.setStringParameterNoEscape('caller_id', current.caller_id);
// note that this isn't listed in REST content or variables.
r.setStringParameterNoEscape('description', current.description);
r.setStringParameterNoEscape('number', current.number);
r.setStringParameterNoEscape('u_inc_urgency', current.u_inc_urgency); //Custom urgency field
r.setStringParameterNoEscape('u_inc_impact', current.u_inc_impact); ////Custom impact field
r.setStringParameterNoEscape('short_description', current.short_description);
r.setStringParameterNoEscape('incident_state', current.incident_state);
r.setStringParameterNoEscape('correlation_id', current.correlation_id);
r.setStringParameterNoEscape('u_external_reference_id', current.sys_id);
r.setRequestBodyFromAttachment(target);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}
})(current, previous);
Your help will be much appreciated. Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2022 09:05 AM
Does this function send the attachment in Base64 format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2024 02:10 AM
For text and image files it’s working.pdf and excel files is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 11:44 AM
Hi Josh, you are the best. thank you so much for providing this solution.