How to apply pagination in the scripted rest api Resource for GET call.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:24 AM
Hello All,
I have a custom table X and it has 10 lakh records and the requirement is to create an API to pull all the records with field, value information in JSON format with fieldname as Key and field value as Value.
I have created a Scripted Rest API Resource with HTTP method as GET and written GlideRecord syntax on this table X
eg:
var arr = [];
var gr = new Gliderecord('tablex');
gr.query();
while(gr.next()){
var respData = {};
respData = {field1 : fieldvalue1,
field2 : fieldvalue2,
field3 : fieldvalue3
};
arr.push(respData)
}
response.setBody(arr);
So, I tested this Scripted REST API and I am not able to get the response and transaction failing due to records count is huge. So, I wanted to split the records retrieval in Chunks say 10000 for the first call and next 10000 record in 2nd call and get the records.
How to apply pagination in scripted rest api resource. Please assist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @varunkumar1 ,
Did you got solution for this problem, I am also working on something similiar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
You can use chooseWindow() method of GlideRecord to get this done.
Example
var arr = [];
var gr = new Gliderecord('tablex');
gr.chooseWindow(offset,windowSize)
//offset will be the row number from which you want to fetch the records from
// windowSize will be the number of records you want to fetch
gr.query();
while(gr.next()){
var respData = {};
respData = {field1 : fieldvalue1,
field2 : fieldvalue2,
field3 : fieldvalue3
};
arr.push(respData)
}
response.setBody(arr);
Also, if this is for fetching table data without any specific customized processing of data, you should go for OOTB Table API.
https://www.servicenow.com/docs/r/zurich/api-reference/rest-apis/c_TableAPI.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Dealing with large datasets like that without pagination is just asking for trouble, especially with performance and timeouts. Splitting responses into chunks makes everything way more manageable and scalable. I’ve handled similar cases where proper limit/offset logic saved the API. Even tools like Phonexa rely heavily on structured data flow, so pagination isn’t optional, it’s essential.
