How to go back to the first record for GlideRecord
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2021 10:39 PM
Hi all,
I am using GlideRecord like:
How to go back to the first record for GlideRecord:
var gr_incident = new GlideRecord("incident");
gr_incident.addQuery("xxx");
gr_incident.query();
while(gr_incident.next()) {
}
After the loop, I want to loop the gr_incident from the beginning again, but I don't want to query the db again, is it possible to do that?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2021 10:49 PM
Could you please explain the use-case of the requirement?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2021 11:06 PM
Try to use a double loop instead of read db in loop.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2021 11:30 PM
Hi
Create an empty array and start pushing every GlideRecord into the array.
This way you can access any object be it first or sub sequent at any point of iteration.
Example script:
var incidentArray = [];
var gr_incident = new GlideRecord("incident");
gr_incident.addQuery("xxx");
gr_incident.query();
while(gr_incident.next()) {
incidentArray.push(gr_incident);
// Write your logic
// access any other incident using incidentArray[0], incidentArray[1] etc
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2022 02:24 PM
This will not work. GlideRecord objects are references. Every item in the incidentArray will point to the exact same record.