Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to apply pagination in the scripted rest api Resource for GET call.

varunkumar1
Tera Contributor

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.

3 REPLIES 3

CharanjeetSingh
Tera Contributor

Hi @varunkumar1 ,
Did you got solution for this problem, I am also working on something similiar

FrancisR
Tera Contributor

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

 

 

stevemarkovick
Tera Contributor

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.