- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 09:25 AM
Hi,
I am using sn_ws.RESTMessageV2 in script to read response from one endpoint as below:
var r = new sn_ws.RESTMessageV2('messagename','httpmethod');
var response = r.execute();
var responseBody = response.getBody();
response is in array.
I am getting response body if there less number of records , eg 20 records in array.
but when there are more number of records I am getting response body as blank in script, although I can see it in test http method.
Is there a way to get large response body in script , http method is GET.
Thanks
Deepak
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 05:14 PM
Hi Deepak,
Unfortunately, RESTResponseV2 doesn't have getResponseAsStream() method.
So have to use .saveResponseBodyAsAttachment() method. This will require a table and a record to attach the attachment.
var tablename = '<table name>';
var recordSysId = '<record in table to attach file>';
var filename = '<filename>';
var request = new sn_ws.RESTMessageV2('messagename','httpmethod');
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
Then if necessary use GlideSysAttachment() method to read attachment as a stream to read the content to use in a script. "attachmentSysId" is the sys_id of the file in the sys_attachment table. .saveResponseBodyAsAttachment() method, unfortunately, doesn't return the sys_id so the table needs to be queried using tablename, recordsysId, and filename to find it.
Furthermore, there is a maximum size of attachment.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0718101
and for REST API
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0747638
In all, it is easier to use paging if the REST API at the other site supports it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 05:48 AM
Thanks Markus and Hitoshi,
I am trying to use saveResponseBodyAsAttachment as below to save response as attachemnet.
var tablename = '<table name>';
var recordSysId = '<record in table to attach file>';
var filename = '<filename>';
var request = new sn_ws.RESTMessageV2('messagename','httpmethod');
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
Here I am able to create attachement in sys_attachment table when tablename=incident and recordSysId =valid sys id.
Instead of incident I am trying to use a custom table (insert a record in custom table then pass table name and sysid of record in args) , but this is not working.
Is there any requirement for table to be used in saveResponseBodyAsAttachment parameters ?
Thanks
Deepak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2022 07:03 AM
The regular 'Application Access' rules should apply (Accessible from).
I have tried this with a custom table and I didn't have any problems... can you try gs.info(response.getResponseAttachmentSysid())? Or do you get a certain error message if you run the REST call in a background script?