Retrieve and add attachments - Rest API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 02:48 AM - edited 03-16-2023 03:30 AM
Hello All,
I wanted to retrieve attachments from third party tool and attach it into staging table as a attachment.
I have created Rest message and written script to achieve this but I am able to map only data in staging table not actual attachment .
Here is my script for reference.
var request = new sn_ws.RESTMessageV2(' Demo Rest API', 'Default GET');
request.setHttpMethod("GET");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
response = request.execute();
var responseData=response.getBody();
var responseObj=JSON.parse(responseData);
gs.info(responseData);
var rec=new GlideRecord('u_contracts_attachment');
rec.initialize();
gs.info(responseObj.result[0].file_name);
rec.file_name=responseObj.result[0].file_name;
rec.content_type=responseObj.result[0].content_type;
rec.insert();
Here is my response for more details...
{"result":[{"size_bytes":"97236","file_name":"Pasted Image","sys_mod_count":"1","average_image_color":"","image_width":"","sys_updated_on":"2022-02-25 04:53:39","sys_tags":"","table_name":"incident","sys_class_name":"sys_attachment","sys_id":"c0a298356f102100758ecb512e3ee409","image_height":"","sys_updated_by":"guest","download_link":"https://dev81990.service-now.com/api/now/attachment/c0a298356f102100758ecb512e3ee409/file","content_type":"image/png","sys_created_on":"2021-07-08 23:13:23","size_compressed":"97119","compressed":"true","state":"available","table_sys_id":"9d385017c611228701d22104cc95c371","chunk_size_bytes":"","hash":"","sys_created_by":"admin"}]}
Please help me to achieve this.
Thanks in advance,
Pratiksha

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 03:00 AM
Refer to this below link about attachment in Servicenow
https://www.servicenow.com/community/itsm-articles/attachments-in-servicenow/ta-p/2305632
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 03:02 AM
if you are able to add it to staging table then you can use GlideSysAttachment copy method to copy to the target record
where are you handling this copy logic?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 03:18 AM
Actually currently I am doing this in background script.
But I am not able to add actual attachment in the staging table.
Currently above script mapping only data.
Please check ss below,