Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to send attachment from one instance to other instance using rest api in servicenow?

sangitakumari
Tera Contributor

 Hi ,

I have written the code but not moving attachment from one instance to other instance.

I have used Rest api 

 

sangitakumari_0-1670652008959.png

 

 

sangitakumari_2-1670652599701.pngsangitakumari_3-1670652658735.png

 

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);

23 REPLIES 23

jaheerhattiwale
Mega Sage

@sangitakumari Working solution explained in detail in the below video. Please check it out.

 

https://www.youtube.com/watch?v=ptPlW4tQXEw

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

I have tried already and make staging table when I am  create incident  and pass value from instance to another instance it's won't work 

@sangitakumari Please try using "Rest API Explorer" to add attachment to same instance first and then just change the instance name to target instance

 

jaheerhattiwale_0-1670690819652.png

 

You can check below link as well:

 

https://developer.servicenow.com/dev.do#!/reference/api/rome/rest/c_AttachmentAPI\

 

Please mark as correct answer if this solves your issue

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hi,

I have used this rest API attachment in first instance it's work attachment after I changed instance name and kept target instance name it's work but when I kept the url of rest API of attachment and test it got error and i did the code for attachment it's won't work kindly just to me what to need change there?