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

ccajohnson
Kilo Sage

You may want to go into your custom table and define your search criteria there. Then you can copy the query and use it in an encoded query string:


Generate an encoded query string through a filter


Thanks, but already tried that. date1, start, date2   and end are variables and the encoded query didn't seem to like them...



i.e.


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



gr.addEncodedQuery('date1>=start^date2<=end');



No joy...



The encoded query is fine. Build the complete query and copy it. It replaces and with ^ and or with OR.



Please mark this response as correct or helpful if it assisted you with your question.

Hi there, thanks, but this is the encoded query and it doesn't work: gr.addEncodedQuery('date1>=start^date2<=end');