- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 09:06 AM
What is the difference between if(inc.next()) & while(inc.next()). if ran a query by doing GlideAggregate to get the count of incidents. if i get always some records more than 150 . which one is preferable using if(next()) or using while(next()).
I believe using while loop runs the query for 150 times . Using if is preferable. Am i Correct
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 11:01 AM
Hi,
Yes you are correct. While loop code will run for 120 times and if code will run once.
Thanks,
Ashutosh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 09:18 AM
HI,
So if(inc.next()) this means it will go into loop and execute code only one time and if you use while loop then the code in that while loop will be executed that many time's. The Gliderecord will be executed only once.
Ex:
var gr = new GlideRecord('incident');
gr.addQuery('active',true);
gr.query();
while(gr.next()){
//CODE will execute for the count of records return by your query lets say 150 but query only once
}
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 10:00 AM
If this code is executed (consider it finds 120 records) then it runs for 120 times.
var inc = new GlideAggregate('incident');
var str = 'sys_created_onRELATIVEGE@hour@ago@24^short_descriptionLIKEBomgar';
inc.addEncodedQuery(str);
inc.addAggregate('COUNT');
inc.query();
while (inc.next()) {
var inc_120 = inc.getAggregate('COUNT');
}
Where as if the below code runs, it also finds 120 records but runs for only one time not like the while loop ... am I correct ?
var inc = new GlideAggregate('incident');
var str = 'sys_created_onRELATIVEGE@hour@ago@24^short_descriptionLIKEBomgar';
inc.addEncodedQuery(str);
inc.addAggregate('COUNT');
inc.query();
if (inc.next()) {
var inc_120 = inc.getAggregate('COUNT');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 11:01 AM
Hi,
Yes you are correct. While loop code will run for 120 times and if code will run once.
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-08-2020 10:04 AM
Hi,
while(inc.next()): This will return multiple records, because a while statement is used to cycle through the query results.
Eg.
(function() {
grCI = new GlideRecord('cmdb_ci');
grCI.addQuery('sys_class_name','=','cmdb_ci_rack');
grCI.query();
while (grCI.next()){
gs.log('CI Found: '+grCI.name);
}
})();
if(inc.next()): This will return one record, because a if statement is used to cycle through the query results. This is good if you just want to find one record, however the query would have returned multiple items, which isn't completely efficient.
Eg.
(function() {
var grIncident = new GlideRecord('incident');
grIncident.addQuery('priority','5');
grIncident.addQuery('category','inquiry');
grIncident.query();
if (grIncident.next()) {
gs.log('Incident Found: '+grIncident.number);
}
})();