how to run strict query for gliderecord.

sumitjumale
Kilo Contributor

Hi All,

I have requirement to   get all the records based on the query from the script. Suppose if we use the fields names which are not present in the table, then it should not return any records, But servicenow returning all the records in the table.

Example : status filed is not present on table "TABLE_ABC" . if we query with encodedQuery as "status=true" then its returning all the records present in that table.

So I need servicenow should not return any records if the encoded query is incorrect.

Sample Code:

var ref_rec = new GlideRecord('TABLE_ABC');

ref_rec.addEncodedQuery('invalidquery');

ref_rec.query();

while(ref_rec.next()){

gs.print(ref_rec.getRowCount());

  }

Output:

Its returning total row count as 5000

but since query is invalid, it should not return any records.

Please help me to resolve this issue.

Regards,

Sumit Jumale

9 REPLIES 9

Deepa Srivastav
Kilo Sage

Hi Sumit,



Below thread should answer your quest:-



Encoded queries changed in Helsinki




Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Thanks,
Deepa


Chuck Tomasi
Tera Patron

Hi Sumit,



This should help.



find_real_file.png


Note the misspelled 'acttive' field. Normally this would be ignored and you would get 1750 rows returned. If you enabled strict, you get none.



You asked about a way to throw an error. I haven't tried, but you could try enclosing this in a try/catch block to see if you can trap the error.



Note: gs.getSession().setStrictQuery(boolean) does not appear to be available to scoped apps at the moment.


Hi Chuck,


Here is my post on another thread (Re: Encoded queries changed in Helsinki )


Very appreciate if you can respond to my issue. Thanks.



------------------------------


The logic that used to check whether encoded query is valid or not may not work as expected. I've added the property "glide.invalid_query.returns_no_rows" into [sys_properties] table.


Here is my code running on script-background:


gs.print('glide.invalid_query.returns_no_rows='+gs.getProperty('glide.invalid_query.returns_no_rows'));


gs.getSession().setStrictQuery(true);



var gr = new GlideAggregate('incident');


gr.addAggregate('COUNT');


gr.addQuery('non_exist_id','57edc4fcdb86aa00c3187a8dae9619bc');


gs.print('Encoded query=['+gr.getEncodedQuery()+']');


if (gr.getEncodedQuery() != '') {


  gr.query();


  if (gr.next()) {


  gs.print(gr.getAggregate('COUNT'));


  }


}


gs.getSession().setStrictQuery(false);



And, I got the output:


*** Script: glide.invalid_query.returns_no_rows=false


*** Script: Encoded query=[non_exist_id=57edc4fcdb86aa00c3187a8dae9619bc]


*** Script: 30056



Do you have any idea why it doesn't work?


------------------------------


What is non_exist_id? Is that supposed to be u_non_exist_id?