How to get the document using Scripted REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 05:46 AM - edited 03-06-2024 05:59 AM
Hello,
We have requirement like if we have an attachment in a catalog task, we need to send that attachment to a third-party ServiceNow "attachment" table using Scripted Rest API.
Could anyone please help me with the process and how can I proceed step by step as I am not aware?
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 06:16 AM
This will be the process flow. Also sharing rough sketch on how the scripts might look.
- Create a Scripted REST API on the target instance: This REST API will accept file data and attach it to a specified record. You would use the GlideSysAttachment API to create the attachment.
- Create a Business Rule or a Flow on your instance: This would be triggered when an attachment is added to a Catalog Task. In this script, you would use the GlideSysAttachment API to read the attachment data.
- Send the attachment data to the target instance's REST API: You would use the RESTMessageV2 API to send a HTTP POST request to the target instance's REST API. The attachment data would be included in the request body.
Target Instance - Scripted REST API Resource Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
2 var writer = new global.GlideSysAttachment();
3 var sys_id = request.pathParams.sys_id; // the sys_id of the target record
4 var tableName = request.pathParams.table; // the table of the target record
5 var fileName = request.body.data.fileName;
6 var contentType = request.body.data.contentType;
7 var base64Content = request.body.data.base64Content;
8 var binaryContent = GlideStringUtil.base64DecodeAsBytes(base64Content);
9 writer.write(tableName, sys_id, fileName, contentType, binaryContent);
10})();
Source Instance - Business Rule or Flow Script:
var grAttachment = new GlideRecord('sys_attachment');
2grAttachment.addQuery('table_sys_id', current.sys_id); // current is the Catalog Task record
3grAttachment.query();
4if (grAttachment.next()) {
5 var attachmentContent = GlideSysAttachment.getBytes(grAttachment);
6 var base64Content = GlideStringUtil.base64Encode(attachmentContent);
7
8 var restMessage = new sn_ws.RESTMessageV2();
9 restMessage.setEndpoint('https://target-instance.service-now.com/api/your_rest_api_endpoint');
10 restMessage.setHttpMethod('POST');
11 restMessage.setRequestHeader('Accept', 'application/json');
12 restMessage.setRequestHeader('Content-Type', 'application/json');
13 restMessage.setRequestBody(JSON.stringify({
14 fileName: grAttachment.file_name,
15 contentType: grAttachment.content_type,
16 base64Content: base64Content
17 }));
18 var restResponse = restMessage.execute();
19}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2025 07:58 PM
All Attachments are placed in sys_attachment table and using the table_sys_id attribute in the table, you can tap into the record where the attachment is having symbolink link with.
You can use the OOB attachment API to do this work where you can use the POST attachment API endpoint and trigger that using a business rule/Flow which is going to send that attachment content to this other SN instance.
Hope this helps. Please mark complete if this answers your question so that future readers can learn more.