How to reset GlideRecord object

vaibhavbhatnaga
ServiceNow Employee
ServiceNow Employee

Hello,

I have a code like this

var gr = new GlideRecord('myTable');

for(int i=0;i<5;i++){

        gr.addQuery('column', i)

        gr.query();

        gr.getrowcount();

}

Problem is, for i=0 it works but for next iterator it appends the addQuery condition to previous one. Example

i=0 condition is column=0

i=1 condition is column=0^column=1

.

.

i=4 condition is column=0^column=1^column=2^column=3^column=4

My question is, how I can rest the gr after each loop, so that only one addQuery is added not the previous one?

Thanks!

15 REPLIES 15

Chuck Tomasi
Tera Patron

Add the new GlideRecord inside your for loop.



for(int i=0;i<5;i++){


        var gr = new GlideRecord('myTable');


        gr.addQuery('column', i)


        gr.query();


        gr.getrowcount();


}


Hi Chuck,



Thanks for your response!


In each loop if I create var gr = new GlideRecord('myTable'), will it not be create unnecessary objects?


No, because we haven't saved or committed anything. There is no call to gr.insert() or gr.update().



I also noted that both of us are using getRowCount() improperly (it's not all lower case) and we're not outputting anything. Try this.



for(int i=0;i<5;i++){


        var gr = new GlideRecord('myTable');


        gr.newRecord();


        gr.addQuery('column', i)


        gr.query();


      gs.info(gr.getRowCount() + ' rows returned from query');


}




In actual logic I am going to update a column in the loop. Is it recommendable to use it?