How to go back to the first record for GlideRecord

PhoenixMing0912
Giga Expert

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?

6 REPLIES 6

Ravi Peddineni
Kilo Sage

@PhoenixMing0912 

Could you please explain the use-case of the requirement?

Try to use a double loop instead of read db in loop.

Mahesh Kumar3
Giga Guru
Giga Guru

Hi @PhoenixMing0912 ,

 

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
}

This will not work. GlideRecord objects are references. Every item in the incidentArray will point to the exact same record.