Get attachments through rest message

p t1
Kilo Sage
Kilo Sage

Hi Team,

 

I want to get attachments from one instance to another instance through rest message.

 

For example: Incidents created on this week and if all tickets having an attachment then all attachments should be get in another instance through get method.

 

Can anyone help on this?

 

Thanks

8 REPLIES 8

Hi @Nayan Dhamane 

@Ankur Bawiskar can you please help on this.

Let me explain scenario in better way

 

Catalog Item Name : Computer

 

I am having a 5 requested item for Computer created on this week and all RITM's have an attachment so at one go i want to fetch all attachments from one instance to another instance.

 

Thanks,

This below script can be used to send attachmnet from the instance to other:

 

var RITM = new GlideRecord("sc_req_item");

RITM.addEncodedQuery(add your query for the records you want to send the attachment);

RITM.query();

while(RITM.next()){

       var attachmentRec = new GlideRecord("sys_attachment");

      attachmentRec.addQuery("table_sys_id",RITM.sys_id);

      attachmentRec.addQuery("table_name", 'sc_req_item');

      attachmentRec.query();

                      while (attachmentRec.next()) {

                      var attachmentMessage = new sn_ws.RESTMessageV2();

                      attachmentMessage.setHttpMethod("post");

                      attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);

                      attachmentMessage.setEndpoint(targetInstanceURL + "api/now/attachment/file");

                      attachmentMessage.setQueryParameter("table_name", attachmentRec.table_name);

                      attachmentMessage.setQueryParameter("table_sys_id", targetID); // you might have saved this in the RITM record use it.

                      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 my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

@p t1 

So how it's happening currently?

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

Currently we can get attachment records based on table name and particular sys_id we need to pass.

 

Thanks,

Preethi