Retrieve Attachments - GET API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2021 10:15 PM
Hello,
I am working on pulling attachments from a third party ServiceNow instance to client's ServiceNow. Third party system does not POST the attachments, but we need to pull the attachments on regular intervals.
I am using GET API to retrieve the attachments from the third party to insert them to 'sys_attachment' table. I have a scheduler which run every 2 hours to invoke GET API call via script include.
I am able to insert the record to 'sys_attachment' table, but as guessed these are meta data and there is no content in the file (size bytes = 0).
Can you please suggest if there are any functions available to retrieve the file content too?
I am using this method and passing table_sys_id to retrieve one attachment at a time. (as there is no better way to query the sys_attachment table using company field, creation time OR to pass multiple 'table_sys_id' in the syparm query)
https://<instance>/api/now/attachment?sysparm_query=table_sys_id=${table_sys_id}%5Esys_created_by!%3DrestUser
I referred these articles and they are not exactly related to my requirement.
https://developer.servicenow.com/blog.do?p=/post/saving-restmessagev2-responses-as-attachments/
https://community.servicenow.com/community?id=community_question&sys_id=bc997b5e1be0d0107a5933f2cd4bcba0&view_source=searchResul
Has anyone come across this kind of requirement and got it working? Can you please share your inputs?
Regards,
Viji
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2021 10:34 PM
Hi,
I consider you must be consuming the REST Message for that GET method
To add that received file directly on record you have this method saveResponseBodyAsAttachment()
That should work fine
what error you are getting
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 12:07 AM
Thanks Ankur for the response.
This is my script bit which processes the external response received and inserting to sys_attachment table.
source : my instance
target: third party instance
_________________________________
try {
var r = new sn_ws.RESTMessageV2('Attachment Outbound xxxx', 'GET');
r.setStringParameter('table_sys_id', corID);
//r.saveResponseBodyAsAttachment("incident", tableSysIdSource, "filename1");
var response = r.execute();
GlideStringUtil.base64Encode(response);
var responseBody = response.getBody();
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
for (i = 0; i < parsed.result.length; i++) {
var link = parsed.result[i].download_link;
var grAtt = new GlideRecord('sys_attachment');
grAtt.initialize();
grAtt.table_name = parsed.result[i].table_name;
grAtt.table_sys_id = tableSysId_BW;
grAtt.size_bytes = parsed.result[i].size_bytes;
grAtt.content_type = parsed.result[i].content_type;
grAtt.file_name = parsed.result[i].file_name;
grAtt.insert();
}
}
catch(ex) {
var message = ex.getMessage();
}
_______________________________________________
corID - I first do GET Incidents API call and collect the incidents which are in scope.
Then, loop through the incident in source instance and collect the 'Correlation ID'.
Correlation ID is table sys ID in 'sys_attachment' table in target instance.
I then loop through the Correlation ID and make GET API call to 'sys_attachment' table in target instance.
I was using the function saveResponseBodyAsAttachment in my script above, it didnt make any difference (tableSysIdSource is the sys_id of ticket in source instance). Infact, I am not aware if I am using at the right place. I do not have the 'filename' as it is set before executing the request.
There is no error, Attachment record is inserted and the file is empty when opened.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 12:36 AM
Hi,
So it means the data you are receiving is not in proper format
did you check the response body you got?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2021 02:14 AM
Hi Ankur,
with this function ON - r.saveResponseBodyAsAttachment, the response of the GET call is stored as attachment to the Incident ticket. The actual attachment does not come. there is no entry in sys_attachment table too
without this function - there is an entry in 'sys_attachment' table, but the file content is empty. Example records below (TEST_attachment.txt is the actual file and size bytes is 0 though available in response)
Rresponse received: