How to reset GlideRecord object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2016 03:45 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2016 10:22 AM
Someone has been hanging around the Technical Best Practices pages! Nice job!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2017 12:49 PM
One minor change to the script above, change the for loop int, to var (see bold text below.)
updateMyRecords();
function updateMyRecords() {
for (var i = 0; i < 5; i++) {
var myTableGR = new GlideRecord('myTable');
myTableGR.addQuery('column', i)
myTableGR.query();
gs.info(myTableGR.getRowCount() + ' rows returned from query > '+myTableGR.getEncodedQuery());
while (myTableGR.next()) {
myTableGR.u_field1 = 'some value'; // replace with your real field and values here
myTableGR.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 03:52 AM
Yes you can instantiate a new GlideRecord object within a loop. I have always wanted to know if that has downsides, in terms of resources or performance. There must surely be some overhead to creating new GlideRecord objects. I may be wrong, but at one time I think I read someone saying that it could cause memory problems if a script created large numbers of GlideRecord objects in a loop. Presumably each time round the loop Javascript is releasing and reclaiming the memory used by the previous GlideRecord.
It would seem nicer to create it once on a given table, and then be able to use it multiple times by resetting it and adding new queries each time. Does anyone know if that is possible, or if it's actually needed?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 05:39 AM
For what it's worth, I have not heard of any memory or performance issues on GlideRecord within a loop. I do this all the time.
Instantiating it outside the loop leaves too much risk, IMO, of unknown or unwanted values getting reused on each record if it's not instantiated (followed by a call to initialize() or newRecord(), ideally.) Having a fresh record removes this risk entirely.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2020 05:57 AM
Thanks, Chuck. Something else I thought of is that server side scripts are running in Rhino. Don't really understand what that is doing. I originally thought it converted the Javascript into Java, so that would reduce any resource/performance impact. But looking at the Rhino Wikipedia page it might still be interpreting it rather than compiling it. Either way, I will continue instantiating them as required, and stop worrying about it doing any damage :-).