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 04:49 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
07-16-2024 07:58 AM - edited 07-16-2024 07:58 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 08:46 AM
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.