- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 02:34 AM
Hello Team,
Can you tell me how to add attachment in incident when we create incident in one instance and send it to other instance with rest api.
Please help me with this.
Regards,
Nivedita
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2021 10:43 AM
Hi Nivedita,
here is the sample code that you can put in your BR of incident which will help you to push the attachment along with the incident to another instance.
(function executeRule(current, previous /*null when async*/ ) {
var body = {
"short_description": current.getValue("short_description"),
"description": current.getValue("description"),
"urgency": current.getValue("urgency"),
"impact": current.getValue("impact"),
"state": current.getValue("state")
};
try {
var r = new sn_ws.RESTMessageV2('Create_Incident', 'Create Incident');
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody = response.getBody();
var json = JSON.parse(responseBody);
var remoteInc_sys_id=json.result.sys_id;
var httpStatus = response.getStatusCode();
gs.log("Response status code is " + httpStatus);
if (httpStatus == "201") {
gs.log("Entered into the if conditon");
//Fetch attachment associated with the incident
var att = new GlideRecord("sys_attachment");
att.addQuery("table_sys_id", current.sys_id);
att.query();
//if you want to push all attachmetns, use while loop here
if (att.next()) {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("post");
attachmentMessage.setBasicAuth("username", "password");
attachmentMessage.setEndpoint("https://devxxxxx.service-now.com/api/now/attachment/file");
attachmentMessage.setQueryParameter("table_name", att.table_name);
attachmentMessage.setQueryParameter("table_sys_id",remoteInc_sys_id);
attachmentMessage.setQueryParameter("file_name", att.file_name);
attachmentMessage.setRequestHeader("Content-Type", att.content_type);
attachmentMessage.setRequestHeader("Accept", "application/json");
attachmentMessage.setRequestBodyFromAttachment(att.sys_id);
response = attachmentMessage.execute();
responseBody = response.getBody();
httpStatus = response.getStatusCode();
}
}
} catch (ex) {
var message = ex.message;
}
})(current, previous);
Mark the comment as a correct answer and also helpful once worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2021 12:22 AM
ServiceNow has attachment API which would help you.
Refer below links for help
Copying attachment from one instance to another
Copying attachment from one instance to another+
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2021 04:19 AM
Hello Nivedita,
In the left navigation, look for Rest API Explorer and in that you can check out attachment API.
Use that and it will also provide your script which you can use it as per your requirement.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2021 10:43 AM
Hi Nivedita,
here is the sample code that you can put in your BR of incident which will help you to push the attachment along with the incident to another instance.
(function executeRule(current, previous /*null when async*/ ) {
var body = {
"short_description": current.getValue("short_description"),
"description": current.getValue("description"),
"urgency": current.getValue("urgency"),
"impact": current.getValue("impact"),
"state": current.getValue("state")
};
try {
var r = new sn_ws.RESTMessageV2('Create_Incident', 'Create Incident');
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody = response.getBody();
var json = JSON.parse(responseBody);
var remoteInc_sys_id=json.result.sys_id;
var httpStatus = response.getStatusCode();
gs.log("Response status code is " + httpStatus);
if (httpStatus == "201") {
gs.log("Entered into the if conditon");
//Fetch attachment associated with the incident
var att = new GlideRecord("sys_attachment");
att.addQuery("table_sys_id", current.sys_id);
att.query();
//if you want to push all attachmetns, use while loop here
if (att.next()) {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("post");
attachmentMessage.setBasicAuth("username", "password");
attachmentMessage.setEndpoint("https://devxxxxx.service-now.com/api/now/attachment/file");
attachmentMessage.setQueryParameter("table_name", att.table_name);
attachmentMessage.setQueryParameter("table_sys_id",remoteInc_sys_id);
attachmentMessage.setQueryParameter("file_name", att.file_name);
attachmentMessage.setRequestHeader("Content-Type", att.content_type);
attachmentMessage.setRequestHeader("Accept", "application/json");
attachmentMessage.setRequestBodyFromAttachment(att.sys_id);
response = attachmentMessage.execute();
responseBody = response.getBody();
httpStatus = response.getStatusCode();
}
}
} catch (ex) {
var message = ex.message;
}
})(current, previous);
Mark the comment as a correct answer and also helpful once worked.