How to break GlideQuery forEach loop

TT3
Kilo Guru

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;
           }
	});
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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;
  }
}

View solution in original post

7 REPLIES 7

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!

Carlos Paiva
Tera Contributor

I had this same issue... though not as elegant as I would like, I used .toArray(100) and iterated through the resulting array

Hitoshi Ozawa
Giga Sage
Giga Sage

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;
  }
}