- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-23-2021 12:27 PM
I have below script, I need to exist the loop when variable i is 2. However, break or return statement does not work here and ServiceNow keeps iterating till the end.
Any idea about how to exist foeEach?
var i = 0;
//There are 4 records with this condition
new GlideQuery(Constants.Order_Item_Table)
.where('u_parent_inventory_id', '123')
.select('u_inventory_id')
.forEach(function (orderItem) {
gs.print(orderItem.u_inventory_id);
i=i+1;
gs.print(i);
if (i == 2){
gs.print('in if'); //It prints, but continues with loop
return;
}
});
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-23-2021 02:02 PM
To break out, throw an exception instead of return.
Example:
var BreakException = {};
try {
var i = 0;
//There are 4 records with this condition
new GlideQuery(Constants.Order_Item_Table)
.where('u_parent_inventory_id', '123')
.select('u_inventory_id')
.forEach(function(orderItem) {
gs.print(orderItem.u_inventory_id);
i = i + 1;
gs.print(i);
if (i == 2) {
gs.print('in if'); //It prints, but continues with loop
throw BreakException;
}
});
} catch (e) {
if (e != BreakException) throw e;
}
Else, use GlideRecord instead when there is a need to break.
var i = 0;
var gr = new GlideRecord(Constants.Order_Item_Table);
gr.addQuery('u_parent_inventory_id', '123');
gr.query();
while (gr.next()) {
var id = gr.u_parent_inventory_id;
gs.print(gr.u_inventory_id);
i=i+1;
gs.print(i);
if (i == 2){
gs.print('in if'); //It prints, but continues with loop
return;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-23-2021 01:40 PM
Hi,
Understood, do you really need to use GlideQuery here?
Still a good point to know how to break out of the forEach, I'm looking into it, but for all purposes, you can use GlideRecord and set condition with while loop and it'll break out easily.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2023 03:11 PM
I had this same issue... though not as elegant as I would like, I used .toArray(100) and iterated through the resulting array

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-23-2021 02:02 PM
To break out, throw an exception instead of return.
Example:
var BreakException = {};
try {
var i = 0;
//There are 4 records with this condition
new GlideQuery(Constants.Order_Item_Table)
.where('u_parent_inventory_id', '123')
.select('u_inventory_id')
.forEach(function(orderItem) {
gs.print(orderItem.u_inventory_id);
i = i + 1;
gs.print(i);
if (i == 2) {
gs.print('in if'); //It prints, but continues with loop
throw BreakException;
}
});
} catch (e) {
if (e != BreakException) throw e;
}
Else, use GlideRecord instead when there is a need to break.
var i = 0;
var gr = new GlideRecord(Constants.Order_Item_Table);
gr.addQuery('u_parent_inventory_id', '123');
gr.query();
while (gr.next()) {
var id = gr.u_parent_inventory_id;
gs.print(gr.u_inventory_id);
i=i+1;
gs.print(i);
if (i == 2){
gs.print('in if'); //It prints, but continues with loop
return;
}
}