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

kdk4276
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.

2 REPLIES 2

Mahathi
Mega Sage
Mega Sage

Hi @kdk4276 ,

  • sysparm_limit: to define how many records you get in one call (10000 by default) 
  • sysparm_offset: to define the records to exclude from the query 

sysparm_query=ORDERBYsys_created_on: to sort the list of records based on the created date/time 

For detailed information : https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0727636


If my answer helped in any way, please mark it as Correct & 👍Helpful

Thanks

Mahathi




SK Chand Basha
Giga Sage

Hi @kdk4276 

 

You can use PagedGlideRecord

 

https://snprotips.com/blog/2016/9/1/gliderecord-pagination-page-through-records-with-ease

 

Mark it Helpful and Accept Solution!! If this helps you to understand.