How to write EncodedQuery correctly

Jieun Jeong
Giga Contributor

I'm trying to write an Encoded Query.

The situation that start date and end date are earlier than date A

and the situation that start date and end date are after date B

I would like to write a encoded query that can obtain results except for the above two cases.

ex)

gr.addEncodedQuery( !('start_date<A ^ end_date<A'));
gr.addEncodedQuery( !('start_date>B ^ end_date>B'));

The above query is a query that  I wrote as an example. I don't know how to write it.

Does anyone know how to write the right query?

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

My bad. Re-read the question,

The total situation is (start_date, end_date <= b) or (start_date, end_date >= A).

Would need to query twice and combine the results.

That is

var resultArray = [];
var gr1 = new GlideRecord('<table name>');
gr1.addQuery('start_date', '<=', B);
gr1.addQuery('end_date', '<=', B);
gr1.query();
while(gr1.next()) {
  resultArray.push(grs1.sys_id.toString());
}
var gr2 = new GlideRecord('<table name>');
gr2.addQuery('start_date', '>=', A);
gr2.addQuery('end_date', '>=', A);
gr2.query();
while(gr1.next()) {
  resultArray.push(grs2.sys_id.toString());
}

View solution in original post

5 REPLIES 5

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

The easiest way to write encoded queries is to set up a filter condition on a list and copy the query.

 

Right click on the query and select "Copy query". I don't think there is an operator to exclude so have to negate the query condition.

find_real_file.png

That is, instead of trying to do below,

! ('start_date<A ^ end_date<A')

try

'start_date>=A ^ORend_date>=A'

Hitoshi Ozawa
Giga Sage
Giga Sage

My bad. Re-read the question,

The total situation is (start_date, end_date <= b) or (start_date, end_date >= A).

Would need to query twice and combine the results.

That is

var resultArray = [];
var gr1 = new GlideRecord('<table name>');
gr1.addQuery('start_date', '<=', B);
gr1.addQuery('end_date', '<=', B);
gr1.query();
while(gr1.next()) {
  resultArray.push(grs1.sys_id.toString());
}
var gr2 = new GlideRecord('<table name>');
gr2.addQuery('start_date', '>=', A);
gr2.addQuery('end_date', '>=', A);
gr2.query();
while(gr1.next()) {
  resultArray.push(grs2.sys_id.toString());
}