- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2025 02:28 AM
Hi,
I am configuring sending attachments from Incident (ServiceNow) to Other ServiceNow instances using the custom app. I configured the below script in BR. But it is always failing with 401 errors. (As below)
{"error":{"message":"Invalid file type: application/json","detail":null},"status":"failure"}
------------------------BR Script-------------------
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2025 01:24 AM
did you check the links I shared yesterday?
Unable to send attachment via REST
Business rule to send attachment to 3rd Party Tool via REST
Send attachment via rest message
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2025 05:38 AM
Thanks All,
We are missing adding these parameters in the rest endpoint. (highlighted in red). Only sent over BR but missed adding in the end point. It's working now.
https://******.service-now.com/api/now/attachment/file?table_name=${table_name}&table_sys_id=${table_sys_id}&file_name=${file_name}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2025 02:56 AM
Try this as your script, but also check the authentication. 401 normally means that your authentication is incorrect.
var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id; var att = new GlideRecord("sys_attachment"); att.addEncodedQuery(qry); att.query(); while (att.next()) { try { var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment'); // Ensure authentication is set up in the RESTMessageV2 record r.setStringParameterNoEscape('table_sys_id', current.sys_id); r.setStringParameterNoEscape('table_name', 'incident'); r.setStringParameterNoEscape('file_name', att.file_name); // Explicitly setting headers r.setRequestHeader("Accept", "application/json"); r.setRequestHeader("Content-Type", "multipart/form-data"); // Correct MIME type for file upload // Attach the file r.setRequestBodyFromAttachment(att.sys_id); var response = r.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); gs.info("Attachment Update Response Body: " + responseBody); gs.info("Attachment update status: " + httpStatus); } catch (ex) { gs.error("Error sending attachment: " + ex.message); } }
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2025 03:25 AM
Thanks for the Quick response. Getting below error. I am trying to attach a jpeg file.
: {"error":{"message":"Invalid file type: image/jpeg","detail":null},"status":"failure"}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2025 03:03 AM
Hello @balaji_prusty1 ,
var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
var att = new GlideRecord("sys_attachment");
att.addEncodedQuery(qry);
att.query();
while (att.next()) {
try {
var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment');
// Set the required parameters
r.setStringParameterNoEscape('table_sys_id', '611b0822fbb392103bf0f471beefdc06'); // sys_id hardcoded to test
r.setStringParameterNoEscape('table_name', 'incident');
r.setStringParameterNoEscape('file_name', att.file_name);
// Set the request headers correctly
r.setRequestHeader("Accept", "application/json");
r.setRequestHeader("Content-Type", att.content_type); // Use the correct content type
// Attach the request body from the attachment
r.setRequestBodyFromAttachment(att.sys_id);
// Execute the request and handle the response
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("Attachment Update Response Body : " + responseBody);
gs.info("Attachment update status : " + httpStatus);
} catch (ex) {
var message = ex.message;
gs.error("Error sending attachment: " + message);
}
}
Please try the above script
If my response helps you please mark it as helpful/correct.
Thanks In advance.
Regards,
Debasis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2025 03:22 AM
Thanks for the Quick response. Getting below error. I am trying to attach jpeg file.
: {"error":{"message":"Invalid file type: image/jpeg","detail":null},"status":"failure"}