Paginated GlideRecord Utility

Download: Version 1.0

The update set is being created. Please check back in an hour or two for an updated link. 

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: 

Parameter Type Required Description
tableName string Mandatory The system name (such as incident or sc_request) of the table you'd like to work with.
queryString string Optional An encoded query to be used to filter the records returned from the table in the first argument. If none if specified, the default is a blank string, and all records will be returned.
pageSize string Optional The number of records to return per page. If this value is not specified, 500 will be used.

Returns: A new instance of the PagedGlideRecord object.

Example

var pgr = new PagedGlideRecord('sc_req_item', 'state=complete', 1000);

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. 

Parameter Type Required Description
callbackFunction Object (function) Semi-optional The callback function you wish to be called for each GlideRecord returned. Note that this callback function must accept one argument: a GlideRecord object containing the details of a single record.
This is noted as semi-optional because before the .nextPage() method can run, it requires a callback function has been specified. However, you only need to specify it once, and you can do so either as a parameter when calling .nextPage(), or by calling .setCallback().

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

Examples

Simple, single-page usage

function myCallBack(gr) {
  //do stuff
}
var pgr = new PagedGlideRecord('sc_req_item', 'state=complete', 1000);
pgr.nextPage(myCallBack); //Gets just the first page

Looping over each page

var pgr = new PagedGlideRecord('sc_req_item', '', 100); //ALL records, 100 per page
while (pgr.nextPage(myCallBack)) { //Loops until there are no records/pages left.
  //Do some stuff, such as indicate the page number in the UI.
}

Specifying the callback function in advance

var pgr = new PagedGlideRecord('sc_req_item'); //get ALL records in table, don't care about page size
pgr.setCallback(myCallBack); //Specify callback function
while (pgr.nextPage()) { //Loops until there are no records/pages left.
  //Do some stuff
}

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. 

Parameter Type Required Description
callbackFunction Object (function) Semi-optional The callback function you wish to be called for each GlideRecord returned. Note that this callback function must accept one argument: a GlideRecord object containing the details of a single record.
This is noted as semi-optional because before the .nextPage() method can run, it requires a callback function has been specified. However, you only need to specify it once, and you can do so either as a parameter when calling .nextPage(), or by calling .setCallback().

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

var pgr = new PagedGlideRecord('sc_req_item'); //get ALL records in table, don't care about page size
pgr.setCallback(myCallBack); //Specify callback function
while (pgr.nextPage()) { //Loops until there are no records/pages left.
  //Do some stuff
}

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