How to send attachment from one instance to other instance using rest api in servicenow?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2022 10:17 PM
Hi ,
I have written the code but not moving attachment from one instance to other instance.
I have used Rest api
i have written code but not moving from one instance to other instance ,kindly provide me is it correct if not correct then provide step by step .
(function executeRule(current, previous /*null when async*/ ) {
try {
var incObj = {};
incObj.caller_id = current.caller_id.toString();
incObj.state = current.state.toString();
incObj.short_description = current.short_description.toString();
incObj.correlation_id = current.sys_id.toString();
incObj.work_notes = current.work_notes.getJournalEntry(-1);
incObj.attachment = b64attachment;
incObj.file_name = attachment.getValue('file_name');
incObj.comments = current.comments.getJournalEntry(-1);
//var tosend = JSON.stringify(body);
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', 'incident');
attachment.addQuery('table_sys_id', current.sys_id);
attachment.query();
var b64attachment = '';
if (attachment()) {
var attchmentIS = new GlideSysAttachmentInputStream(attachment.sys_id);
var bytearrayOS = new Packages.java.io.ByteArrayOutputStream();
//write byte stream*/
attachmentIS.writeTo(bytearrayOS);
/*encode attachment file*/
b64attachment = GlideBase64.encode(bytearrayOS.toByteArray());
/*ebond the ticket*/
var r = new sn_ws.RESTMessageV2('Create in dev2 instance', 'attachment');
r.setRequestBody(JSON.stringify(incObj)); // sending the data to the REST message.
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var crid = JSON.parse(responseBody);
current.correlation_id = crid.result.sys_id.toString();
gs.log("request body:" + responseBody);
}
} catch (ex) {
var message = ex.message;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 09:13 PM
Hi ,
I have comment no 10 and 11 but its won't work and tested but not attched second instance .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 09:17 PM
@sangitakumari The exact requirement is to create a incident in target servicenow instance when a new incident is created in source instance and attach all the attachments from source instance to target instance?
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 08:47 PM
@sangitakumari In line number 11 you are using attachment.getValue('file_name'); but attachment is not defined any where above. Please check that as well. That might be the issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 08:58 PM
what should i now need to delete the code of line number 11??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 09:03 PM
@sangitakumari Please check line no. 10 and 11 of business rule. b64attachment and attachment variables are not defined.
And if we are passing the attachement using setRequestBodyFromAttachment (line no. 51) we can remove line 10 and 11.
So please comment line number 10 and 11 and try.
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023