Get attachments through rest message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2023 03:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2023 03:22 AM
Hi,
The Attachment API is well documented here: https://docs.servicenow.com/en-US/bundle/tokyo-api-reference/page/integrate/inbound-rest/concept/c_A...
Regards,
Niklas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2023 11:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2023 11:52 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2023 12:28 AM
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();
}
}
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.