Pagination in GlideRecord?

robby3
Tera Contributor

I'm trying to figure out if there is a way to do pagination in GlideRecord on my applications UI Page that will have a lot of data on it. I would like to limit to pages of say ~20 records.

After reading the documentation on the Wiki, setLimit and setLocation seemed to be the key. (Coming from MySql, this seems like "limit 0,20", exactly what I want to do.)

var oGR = new GlideRecord('XXXX-SomeTable');

oGR.addQuery('sys_id', sSysID);

oGR.setLimit(iItemsPerPage);

oGR.setLocation(iStartLocation);

oGR.query();    

but this returns "Function setLocation is not allowed in scope"


What are the alternatives to do this? I would really like to restrict my query to only returning the records I need. I could kludge it by getting the full recordset, then just limiting my rendering code, but it's a huge waste of system resources.


What are the best practices to do pagination in an application UI page.   There appears to be pagination on the kb_list UI page, though I have not been able to figure out how it's working yet... I think since it's scoped as global, it's a bit different.


Thanks!

-Robby




1 ACCEPTED SOLUTION

robby3
Tera Contributor

I just figured out the answer  



oGR.chooseWindow(iStartLocation, iEndLocation);



This works a similar way to "limit" does in MySql, but the second variable is the stop point, not the number of items.     I wish they would have listed this in the GlideRecord Wiki documentation page.



Hope this helps someone else having to decode kb_list


View solution in original post

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Robby,



This is not allowed in Fuji. Please refer the Scope GlideRecord API Reference


Scoped GlideRecord API Reference - ServiceNow Wiki


robby3
Tera Contributor

I just figured out the answer  



oGR.chooseWindow(iStartLocation, iEndLocation);



This works a similar way to "limit" does in MySql, but the second variable is the stop point, not the number of items.     I wish they would have listed this in the GlideRecord Wiki documentation page.



Hope this helps someone else having to decode kb_list


michal_v
Giga Contributor

Hi Robby,


I'm trying do a similar thing.


I built a custom search field and I want to separate the results to pages,


I tried using the kb_list ui page as a reference, but it's not working.


This is the way I've coded the search:




var search = RP.getParameterValue('search_term');


  var enc="availability!=on_mobile^no_search=false^active=true^sys_class_name!=sc_cat_item_content^sc_catalogs=e0d08b13c3330100c8b837659bba8fb4";


  var gr = new GlideRecord("sc_cat_item");


  gr.addEncodedQuery(enc);


  gr.addQuery("IR_AND_OR_QUERY",search);


  gr.chooseWindow(${sysparm_current_row - 1}, ${sysparm_current_row + sys_nav_count - 1});


  gr.query();




I get the first 20 results but when i press "next- page" the page is being reload, a parmeter is added to the url (sysparm_current_row=21)


but i still see just the first 20 results.



Maybe You have an idea what I'm doing wrong?


Community Alums
Not applicable

Hi, Have you got output for the above code? do we have to define sysparm_current_row some where since I am trying to get similar to this? Request you to help