Paginated GlideRecord Utility

Download: Version 1.0

Related Article


This utility allows you to get paginated (page-by-page) results from a GlideRecord query in ServiceNow. You can specify the number of records to pull per page, and iterate through them at will just like you can iterate through a GlideRecord.
This might be useful for scripting a large REST query response, accessing a very large number of records in a table, building a UI that lets you sift through records one page at a time, or a multitude of other purposes. 

Usage - Instantiation

The first step to using the PagedGlideRecord class, is declaring your own PagedGlideRecord object. To do so, simply use the new keyword, and pass the following arguments into the Constructor: 

Returns: A new instance of the PagedGlideRecord object.

Example


Once you’ve instantiated your own instance of the PagedGlideRecord class, and passed in the necessary argument(s) to the constructor, you can call its’ methods. Below, you’ll find information on the public methods 

nextPage(Object callbackFunction)

After initializing a PagedGlideRecord object, you can call nextPage() to move to the next (or first) page. Note that the callback function will be called for each record, not for each page. Calling .nextPage() once and getting a result of 30 records, will pass 30 GlideRecord objects into your callback function one at a time. That is, your callback function will be called 30 times. As the article describes, this would be trivial to modify if you’d prefer different functionality, but the performance is virtually identical. 

Returns: A boolean value indicating whether or not there are any records on the next page

Examples

Simple, single-page usage

Looping over each page

Specifying the callback function in advance


setCallback(Object callbackFunction)

You can use the setCallback function to manually set (or re-set!) the callback function that’s called for GlideRecord objects found by the PagedGlideRecord class. 

Returns: A boolean value. If the callback function specified was valid, returns true. If it was invalid but there’s already an acceptable value saved to the class instance, it returns false but doesn’t halt execution. If it’s not valid and there is no current valid callback function specified, it throws an error. 

Example


getPage()

Returns: A number (integer) with the current page that the class instance is on.
If .nextPage() has not yet been run, getPage() will return 0


getCurrentLocation()

Returns: A number (integer) with the (zero-based) location index of the last found record. that the class instance is on.
If .nextPage() has not yet been run, getCurrentLocation() will return -1.

Search