Business rule to Send Attachments through Rest message Integration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2024 11:38 PM
Hi All,
The script below is used for sending attachments from one instance to another. The client provided a GET API to retrieve the sys ID of the target record and a POST API to use the sys ID for updating the attachment in the client's instance. Unfortunately, the script I've written is not working as expected.
Get Api : https://instance_name.service-now.com/api/now/v2/table/incident?sysparm_fields=correlation_id%2Csys_...u_external_number=${case_id}
function executeRule(current, previous /*null when async*/) {
var answer = "";
var attachmentMsg = "";
var r = new sn_ws.RESTMessageV2('Ca_Update', 'Default GET');
r.setStringParameterNoEscape('case_id', current.number);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var targetRec = parsed.result;
var attachmentCount = sendAttachments(current.getTableName(), current.sys_id, targetRec.sys_id);
if (attachmentCount !== "none") {
attachmentMsg = " Attachments successfully sent: " + attachmentCount[0].toString() + ". Attachments failed to be sent: " + attachmentCount[1].toString();
} else {
attachmentMsg = " Record had no attachments to send.";
}
if (attachmentMsg === " Record had no attachments to send.") {
answer = "Incident could not be sent.";
}
answer = answer + attachmentMsg;
// Set message at the top of the screen with results.
gs.addInfoMessage(answer);
}
function sendAttachments(sourceTable, sourceID , targetID) {
var answer = [0, 0]; // successful attachments, failed attachments
// Query for any attachments on the current record.
var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery("table_sys_id", sourceID);
attachmentRec.addQuery("table_name", sourceTable);
attachmentRec.query();
if (attachmentRec.hasNext()) {
var r = new sn_ws.RESTMessageV2('Ca_Update', 'Send Attachment');
r.setStringParameterNoEscape('table_sys_id',targetID);
r.setStringParameterNoEscape('table_name', 'incident');
r.setStringParameterNoEscape('file_name', attachmentRec.file_name);
r.setRequestHeader("Content-Type", attachmentRec.content_type);
r.setRequestHeader("Accept", "application/json");
r.setRequestBodyFromAttachment(attachmentRec.sys_id);
var response = r.execute();
var httpStatus = response.getStatusCode();
if (httpStatus.toString() == "201") {
answer[0] += 1;
gs.log("Test attachment success " + httpStatus);
} else {
answer[1] += 1;
}
}
return answer;
}
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2024 06:24 AM
Do you have details on what errors you are seeing?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2024 12:35 PM
Hi, I hope - the below statement is having issues, Pls chk the connectivity.
Ca_Update
Suresh.