I am getting error 400 while sending attachment using the below code. Request to kindly help.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 09:49 AM - edited 06-12-2025 09:50 AM
The business rule code :
(function executeRule(current, previous /*null when async*/) {
var targetUserID = "******";
var targetUserPassword = "******";
var text = "";
var attachmentMsg = "";
try {
var r = new sn_ws.RESTMessageV2("KB_Bonding", "Create record");
r.setStringParameter('sys_id', current.sys_id);
r.setStringParameter('kb_knowledge_base', current.kb_knowledge_base.toString());
r.setStringParameter('short_description', current.short_description);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus.toString() === '201') {
text = "Knowledge created successfully.";
var parsed = JSON.parse(responseBody);
var targetRec = parsed.result.sys_id;
//gs.log(targetRec, 'Target KB sys_id');
var attachmentCount = sendAttachments(current.getTableName(), current.sys_id.toString(), targetRec);
gs.log(attachmentCount+"-"+current.getTableName()+"-"+current.sys_id.toString()+"-"+targetRec,"<attachmentCount>");
if (attachmentCount > 0) {
attachmentMsg = "Attachments successfully sent: " + attachmentCount;
} else {
attachmentMsg = "Record has no attachments.";
}
} else {
text = "KB could not be created. Status code: " + httpStatus;
}
} catch (ex) {
text = "Exception occurred: " + ex.message;
}
gs.addInfoMessage(text + " " + attachmentMsg);
// Function to copy attachments
function sendAttachments(sourceTable, sourceID, targetID) {
var count = 0;
var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery('table_sys_id', "c7f0f1d2934aa210db9d7af08bba1050");
attachmentRec.addQuery('table_name', "kb_knowledge");
attachmentRec.query();
//gs.log("ok","<ok_2>");
while (attachmentRec.next()) {
gs.log(sourceTable+","+targetID+","+attachmentRec.file_name,"<ok_3>");
var amsg = new sn_ws.RESTMessageV2();
amsg.setHttpMethod("post");
amsg.setBasicAuth(targetUserID, targetUserPassword);
amsg.setEndpoint(targetInstanceURL + "api/now/attachment/file");
gs.log(targetInstanceURL + "api/now/attachment/file","<attachment_api>");
amsg.setQueryParameter("table_name","kb_knowledge");
amsg.setQueryParameter("table_sys_id","c7f0f1d2934aa210db9d7af08bba1050");
amsg.setQueryParameter("file_name",attachmentRec.file_name);
gs.log(attachmentRec.file_name,"<attachmentRec.file_name>");
amsg.setRequestHeader("Accept", "application/json");
amsg.setQueryParameter("Content-Type","multipart/form-data");
amsg.setRequestBodyFromAttachment(attachmentRec.sys_id);
gs.log(attachmentRec.sys_id,"attachmentRec.sys_id");
var response = amsg.execute();
var respStatus = response.getStatusCode();
gs.log(respStatus,"<respStatus>"); //here I get error 400
if (respStatus === 201) {
count++;
} else {
gs.log("Failed to send attachment: " + attachmentRec.file_name + ". Status: " + respStatus);
}
}
return count;
}
})(current, previous);
The article gets created but the attachment does not go.
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 10:03 AM
Hello, in sendAttachments function
this is not query parameter: amsg.setQueryParameter("Content-Type","multipart/form-data");
it should be header amsg.setRequestHeader("Content-Type", "multipart/form-data");