addOrCondition in Script Include

paulhooper
Kilo Contributor

Hi there,

I have a script include within a scoped application. I need to iterate through the records of a table and and populate an array with a specific attribute.

i.e.

var records= [];

var gr = new GlideRecord("u_group_records");

gr.addQuery('date1', '>=', start).addCondition('date2', '<=', end);

gr.query();

while(gr.next()){                                                            

records.push(gr.name.getDisplayValue());

}

However I need to test multiple OR conditions and would have expected the following to work:

var records= [];

var gr = new GlideRecord("u_group_records");

gr.addQuery('date1', '>=', start).addCondition('date2', '<=', end)

    .addOrCondition('date1', '<', start).addCondition('date2', '>', end)

    .addOrCondition('date1', '<', start).addCondition('date2', '<', end).addCondition('date2', '>', start);

gr.query();

while(gr.next()){                                                            

records.push(gr.name.getDisplayValue());

}

All this does is test the first conditions and then ignore the rest.

If I create multiple queries with an individual condition in each, I'm able to get the desired result, however this seems inefficient...

Any advice much appreciated.

1 ACCEPTED SOLUTION

I believe Sanjiv means to do something like this:



var eq = 'date1>=' + start + '^date2<=' + end;


gr.addEncodedQuery(eq);



Although this would work too: gr.addEncodedQuery('date1>=' + start + '^date2<=' + end);



You have to include variables outside of the quotes and concatenate them where needed with the '+'


View solution in original post

6 REPLIES 6

I believe Sanjiv means to do something like this:



var eq = 'date1>=' + start + '^date2<=' + end;


gr.addEncodedQuery(eq);



Although this would work too: gr.addEncodedQuery('date1>=' + start + '^date2<=' + end);



You have to include variables outside of the quotes and concatenate them where needed with the '+'


Ah that makes sense now. Many thanks for your help!