Get attachments through rest message

p t1
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

9 REPLIES 9

Nayan  Dhamane
Kilo Sage

Hello @p t1 ,

By reading your previous conversation. I think that there can be two cases where you need to send the attachments to other instance:

 

1. Whenever a new attachment is added to the RITM.

2. Already existing attachments present on the RITM to be posted.

 

The above conditions can be achieved with Business rule or Scheduled job respectively (assuming we already have the target sys_id stored somewhere here are code snippets which might work for you):

 

1. Business rule(async on attachment table . Kindly add the required conditions):

var RITM = new GlideRecord("sc_req_item");

RITM.addQuery('sys_id',current.table_sys_id);

RITM.query();

if (RITM.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();

}

 

 

2. Scheduled job (or any background script)

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.

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