Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to combine two queries into one result set without encoded query.

Stephen W_
Giga Guru

Pretty basic question I think.. but I'm drawing a blank:

u_somefield=NULL^request_lineISNOTEMPTY^NQu_anotherfield=9697d9046f3a3100f24ced250d3ee40f

Encoded queries are not fun for maintenance, so I try to stick to "addQuery()" type functions.

The above query is really two queries, or a single query with a level of nesting.

u_somefield=NULL^request_lineISNOTEMPTY

+

u_anotherfield=9697d9046f3a3100f24ced250d3ee40f

How can I write this using standard addQuery/addNull.. etc with the appropriate nesting?

Thanks!

1 ACCEPTED SOLUTION

This might be a bit simpler.



var gr = new GlideRecord('table');


gr.addQuery('u_somefield', NULL);


gr.addQuery('request_line', '!=', NULL);


var queryTwo = new GlideRecord('table');


queryTwo.addQuery('u_anotherfield', '9697d9046f3a3100f24ced250d3ee40f');


gr.addEncodedQuery("^NQ" + queryTwo.getEncodedQuery());


gr.query();


while(gr.next()){


//Process


}



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

16 REPLIES 16

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

gr.addQuery('u_somefield','');


gr.addQuery('request_line','!=','');



etc..



All these are "and" and if you want "or" use addOrCondition.



Hope this clears it up..



/Göran


Stephen W_
Giga Guru

Nope.. that's just a basic query.  


I'm looking for nested queries like in my original example, note the bold text in the example.



"NQ" is very different from "^OR"



Thanks


-Stephen


Hi Stephen,



Please refer the below link. Just copy the query from the filter and pass it as addQuery.


http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings


Sharing what I have learned recently!  

 

Be careful with ^NQ in your queries.  When they are used SN does not use the Indexes of the table.  Instead the system does table scans. 

 

Striking right?!?!   Read item number 6 here -> https://www.servicenow.com/community/developer-articles/performance-best-practice-for-efficient-quer...