sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:    BEIGINNER
Assumes good beginner level knowledge and/or familiarity of Scripting in ServiceNow.


Here is a simple way to reset the record pointer back to the beginning (before the first record) using the function restoreLocation on a GlideRecord without having to re-pull the entire recordset.

 

var incidentRecords = new GlideRecord('incident');
incidentRecords.addActiveQuery(); // same as .addQuery('active', true) but fancier :-)
incidentRecords.setLimit(10);   // did you know you could grab just the first x-number of records?
incidentRecords.orderBy('number');   // order ascending
incidentRecords.query();

// print off the first 10 incident numbers
while (incidentRecords.next()) {
    gs.info('Incident number: {0}', incidentRecords.number);
}

// print off the 10th one again, we are still at the end
gs.info('Current Incident number: {0}', incidentRecords.number);

// reset the pointer to beginning (before the first record)
incidentRecords.restoreLocation();

// print off the first 10 incident numbers starting with the first
while (incidentRecords.next()) {
    gs.info('Incident number: {0}', incidentRecords.number);
}

 

Which will give you a result like this:

sabell2012_0-1696254684063.png

So, what is going on here?

  1. First, we do a grab of ten records out of the Incident table, and order them by the number field. At this point the pointer is at the -1 record location (the recordset is zero based, but we can't get at the enumeration - which is there under-the-hood, but we are not allowed access!).
  2. We loop through the entire recordset, and print off all the incident numbers.
  3. We print the last one again to show we have reached the end of the recordset.
  4. We then reset the recordset pointer to the -1 location again, which allows us to use the .next() to once again loop through the same recordset.

There are use-cases where this is desirable; and keeps you from a performance hit where you have to go do another round-trip to the server for a recordset. The default is the -1 location, but you can use saveLocation to put a book mark to reset to anywhere in the recordset.

 

So, this is a useful tool!

 

Be warned: This useful feature is only available in Global. It is not present in the Scoped app version of GlideRecord!

 

Here is the type of error you WILL get if you try to use this feature in a scoped application:

sabell2012_3-1696255583048.png

 

Also, it appears that ServiceNow has mysteriously removed most of the documentation on these features (saveLocation, restoreLocation).

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in and mark it as "Helpful"!

 

sabell2012_1-1696102033098.png


Originally published on: 08-09-2015 04:05 PM

I updated the code and brought the article into alignment with my new formatting standard.