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

Ahh, that's an interesting way to do it.   Write two simple queries and extract the encoded query to combine them.


Benefit of the encoded query with the distinctive query elements of addQuery().



There are cases where that could be easier to maintain than a long encoded query..



Did you test this?   Does addEncodedQuery always slap it on to the end of the existing resulting encodedQuery?


Yeah, I tested it out.


It appends it to the existing query.



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

Hi Stephen,

Fix for incorrect reference links while using (^NQ) operator : https://community.servicenow.com/community?id=view_idea&sysparm_idea_id=26aaaafddb4011d45205e6be1396...

Solution : Whenever we use ^NQ operator to join multiple queries to build a master query

we need enter ^current.getEncodedQuery() at every (^NQ) operator JOINs before moving to final encoded query


Example :


Levelcheck1 : hr_service.u_service_entity=3ef7cbc287e68d900d9f7447cebb359d

Levelcheck2 : hr_service.u_service_entity=0e974b8287e68d900d9f7447cebb35e2^assignment_group=fe7be44b8751b0100d9f7447cebb3515

Levelcheck3 : number=HRC0052382^ORnumber=HRC0001703



Mergedquery1 = Levelcheck1+^NQLevelcheck2+ "^" + current.getEncodedQuery();
Mergedquery2 = Mergedquery1+^NQLevelcheck2+ "^" + current.getEncodedQuery();


current.addEncodedQuery(Mergedquery2)



Regards,
Deepak R

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...

The SN Nerd
Giga Sage
Giga Sage

Please excuse the crude screenshot



2016-02-12_10-27-24.png


active=true^short_descriptionLIKEbla^ORshort_descriptionLIKEhello^NQactive=false^short_descriptionLIKEkit



^ = AND


^OR = OR


^NQ = OR



Hope this makes sense



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