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

Niklas Peterson
Mega Sage
Mega Sage

Hi @Niklas Peterson 

 

Scenario : I have one catalog item and there few attachments in those in RITM, I want to fetch only those through rest api.

 

Thanks,

Preethi

Hi,

You need to spend some time on the design on this. There are decisions that needs to be made. Like should you push the data from one instance to the other or do you want to pull. Do you want to run it scheduled or should you have the attachments sync as soon as they are added etc.

Have a look at the solution of this old community post for some inspiration.
https://www.servicenow.com/community/developer-forum/copying-attachment-from-one-instance-to-another...

 

Regards,

Niklas

Nayan  Dhamane
Kilo Sage
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.