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-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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:06 AM
@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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:24 AM
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();
}
}
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:35 AM
So how it's happening currently?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 12:44 AM
Currently we can get attachment records based on table name and particular sys_id we need to pass.
Thanks,
Preethi
