Copy attachment from one instance to another
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 08:31 AM
Hi All,
In the below script everything is working except the attachment from one instance to another is not copying.
The attachment is not getting copied the script is failing here in this function **Query for any attachments on the current record**
Please provide your inputs on this.
******************************************************************************
(function executeRule(current, previous /*null when async*/ ) {
var answer = "";
var attachmentMsg = "";
//Create a JSON string with the current incident attributes which will fill in incident metadata
var incidentJSON = {};
incidentJSON.caller_id = current.caller_id.toString();
incidentJSON.category = current.category.toString();
incidentJSON.impact = current.impact.toString();
incidentJSON.urgency = current.urgency.toString();
incidentJSON.priority = current.priority.toString();
incidentJSON.short_description = current.short_description.toString();
incidentJSON.u_opposing_incident = current.getValue('number'); //send current incident number to user defined field
incidentJSON.comments = current.comments.getJournalEntry(-1).toString(); // send all additional comments
incidentJSON.correlation_id = current.sys_id.toString(); // sys_id to opposing instance as correlation_id
// Encode JSON string
var requestBody = new global.JSON().encode(incidentJSON);
// Create incident in target instance via REST
//var restMessage = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
var restMessage = new sn_ws.RESTMessageV2('create in ashwin instance', 'create it in ashwin instance');
restMessage.setRequestHeader("Content-Type", "application/json");
restMessage.setRequestBody(requestBody);
var response = restMessage.execute();
var responseBody = response.getBody();
//get variables for populating correlation ID, customer incident number
//send visible message to operator and update incident record with details received
var httpStatus = response.getStatusCode();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var foundSysID = parsed.result.sys_id; //decoded newly created incident sys_id on opposing instance
var foundNumber = parsed.result.number; // decoded newly created incident number on opposing instance
current.correlation_id = foundSysID; //copy the sys_id incident to correlation id
current.u_customer_ticket = foundNumber; //copy oppocing incident number to customer ticket number field
current.u_escalated = 1; // set the escaleted box as true to force UI action for escalation to disappear
current.update(); //update the current incident
if (httpStatus.toString() == "201") {
answer = gs.addInfoMessage("This incident has been escalated to opposing instance : " + foundNumber); //log ticket number and send visible notification to operator
// Get target record's SysID
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.";
}
} else {
answer = "Incident could not be sent.";
}
answer = answer + attachmentMsg;
// Set message at top of screen with results related to attachments
gs.addInfoMessage(answer);
function sendAttachments(sourceTable, sourceID, targetID) {
var answer = [0, 0]; //successful attachments, failed attachments
// Query for any attachments on the current record.
gs.addInfoMessage("table id is"+sourceID);
gs.addInfoMessage("table name is"+sourceTable);
var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery("table_sys_id", sourceID);
//table_sys_id=89a4e7bc2f50a0102e2aea5ef699b6b8
//table_sys_id=df2763b02f90a0102e2aea5ef699b6cc^table_nameSTARTSWITHsc_req_item
attachmentRec.addQuery("table_name", sourceTable);
attachmentRec.query();
if (attachmentRec.hasNext()) {
gs.addInfoMessage("inside if");
while (attachmentRec.next()) {
gs.addInfoMessage("inside while");
// var attachmentMessage = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Attach_Post');
var attachmentMessage = new sn_ws.RESTMessageV2('create in ashwin instance', 'create attachment in ashwin instance');
attachmentMessage.setQueryParameter("table_name", attachmentRec.table_name);
attachmentMessage.setQueryParameter("table_sys_id", targetID);
attachmentMessage.setQueryParameter("file_name", attachmentRec.file_name);
attachmentMessage.setRequestHeader("Content-Type", attachmentRec.content_type);
attachmentMessage.setRequestHeader("Accept", "application/json");
attachmentMessage.setRequestBodyFromAttachment(attachmentRec.sys_id);
var response = attachmentMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus.toString() == "201") {
answer[0] += 1;
} else {
answer[1] += 1;
}
}
} else {
gs.addInfoMessage("inside else");
answer = "none";
}
return answer;
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 08:58 AM
Below solution will be helpful to you
https://community.servicenow.com/community?id=community_question&sys_id=9fa483e9dbd8dbc01dcaf3231f96199e
Regards,
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 09:07 AM
Hi Sachin,
Yes ,I did try this and the other scripts but the attachment is not getting copied
Many Thanks,
Ashwin Umapathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2020 11:35 PM
Thank you ALL.. It worked now
ACL's was the issue for "sys attachment table"