addOrCondition() not working

CollinPope
Kilo Contributor

When I tried to execute the code below, in a Background script, in our Test instance, it only returned a row count of 1.

Both of the POs being queried exist.

When I swap the values in the addQuery() and addOrCondition() functions, whichever 'gr.number' value is in the 'addQuery()' function is the one that is found.

var gr = new GlideRecord('proc_po');

gr.addQuery('number','PO000273');

gr.addOrCondition('number','PO002225');

gr.query();

gr.next();

gs.print(gr.getRowCount() + ':' + gr.number);

3 REPLIES 3

ChrisBurks
Mega Sage

I believe you have to assign one of your queries to a variable and then add the next or condition or you can just dot-walk out to the next query condition. Then if you want to iterate through all that is found you would need to loop through it such as using a while loop or something. These two examples should work:


Example one:


var count = 1;


var gr = new GlideRecord('proc_po');


var qry = gr.addQuery('number','PO000273');


qry.addOrCondition('number','PO0002225');



gr.query();


gs.print( 'Records found: ' + gr.getRowCount());


while(gr.next()){



gs.print( count + ':' + gr.number);


count++;


}



Example two:


var count = 1:


var gr = new GlideRecord('proc_po');


gr.addQuery('number', 'PO000273').addOrCondition('number', 'PO0002225');



gr.query();


gs.print( 'Records found: ' + gr.getRowCount());


while(gr.next()){


gs.print(   count + ':' + gr.number);


count++;


}


CollinPope
Kilo Contributor

Thanks Chris, that works great!



I also found some of examples of this here...


http://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/


salemsap
Tera Expert

Hi,


I can see some issue in code.Refer the below link to know the syntax for addorcondition,


GlideRecord - ServiceNow Wiki