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

Mike Allen
Mega Sage

At the heart of it, this OR query is two separate queries with the result set joined.   So, I guess you'd have to do the same thing here:



var arrayUtil = new ArrayUtil();


var firstQuery = newArray(doTheFirstQuery());


var secondQuery = newArray(doTheSecondQuery());



var results = arrayUtil.concat(firstQuery, secondQuery);



function doTheFirstQuery(){


        var arrayOne = newArray();


        var queryOne = new GlideRecord('table');


        queryOne.addQuery('u_somefield', NULL);


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


        queryOne.query();


        while(queryOne.next()){


                  arrayOne.push(queryOne.sys_id);


        }


        return arrayOne;


}


function doTheSecondQuery(){


        var arrayTwo = newArray();


        var queryTwo = new GlideRecord('table');


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


        queryTwo.query();


        while(queryTwo.next()){


                  arrayTwo.push(queryOne.sys_id);


        }


        return arrayTwo;


}



Something like that.   You could probably just return whatever you want.   I returned arrays and concatenated them.   You could print out strings or whatever, but I think this would be the core concept.


Thanks Mike, that's exactly right.



I appreciate the thoughtful response.   I suppose in this case, encoded queries are really the best option for maintanability.



Thanks again,


-Stephen


Would I be correct in saying that there is no method to join GlideQueryConditions together (ie the ^NQ) and doing to seperates queries with a join is the only way to codify it?



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

I don't know of anything that exists like that.   I try to look at things like this and break it up to try to get at what the application is doing.   My best educated guess is that EQ means 'end query' and NQ means 'new query' in the encoded query.   Going off that assumption, to replicate what the tool is doing, you have to do that.   So, NQ literally means do these two separate queries and join the results, because that is what the tool is doing.   I'm sure there is a better way to do anything I suggest.   I just have not found it in this particular case.


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