How to use setLimit and offset in query?

Hulk1
Giga Contributor

I have a table with 10000 records. I need to retrieve all the data from table and show it in UI page. I add pagination in the UI page(it shows only 50 records in the first page). As of now i wrote a query to retrieve all the records from the table and show only 50 records per page.

var gr=new GlideRecord(table_name);

gr.addQuery(some query here to fetch the data which satisfy the given given condition);

gr.query();     //It fetches all the records which satisfy the condition given in addquery

gr.chooseWindow(1,50);    // It helps to get the records with the query

gr.query();

Here my problem is it takes some time to fetch the records and show it in UI page[performance issue is there]. To overcome the issue i want something like that.

var gr=new GlideRecord(table_name);

gr.addQuery(some query here to fetch the data which satisfy the given given condition);

gr.chooseWindow(1,50); 

gr.query();

I need to fetch only the data given in the limit. Any possible way to do it?

 

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi @Hulk ,

chooseWindow() only makes sense in combination with setLimit(). And please note that numbering of chooseWindow() starts with "0" (not "1"!).

So the result for querying the first 50 records would be as follows:

var gr=new GlideRecord(table_name);

gr.chooseWindow(0, 49); 
gr.setLimit(50);
gr.query();

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

View solution in original post

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi @Hulk ,

chooseWindow() only makes sense in combination with setLimit(). And please note that numbering of chooseWindow() starts with "0" (not "1"!).

So the result for querying the first 50 records would be as follows:

var gr=new GlideRecord(table_name);

gr.chooseWindow(0, 49); 
gr.setLimit(50);
gr.query();

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

SetLimit alongwith choosewindow is not providing the correct result

asifnoor
Kilo Patron

Hi,

Go through this link. This should help you.

https://community.servicenow.com/community?id=community_question&sys_id=e28c0fa5db9cdbc01dcaf3231f96...

Mark the comment as a correct answer and helpful if this helps to solve the problem.