Having trouble understanding why the query operator "^NQ" acts as a grouped OR for record listings

bcronrath
Kilo Guru

I had to build a content page that lists incidents with a particular set of filters in an iframe, and was doing so via building it in the URL contained in the iframe.

I've gotten it to where I wanted it to be, but I don't really understand why it functions the way it does.   My issue came about because I needed to figure a way to group a set of AND statements against other OR statements.   Something like the following:

(Caller is (insert js function here))

OR

(Opened by is (insert js function here))

OR

(Company is not empty

AND Company name is not (insert company name here))

The only way I was able to accomplish this in URL query parameters was by in the sysparm_query value replacing those ^OR instead with ^NQ.   I only came up with this by building the query out in an incident filter and copying the URL it spat back.

Looking at: http://wiki.servicenow.com/index.php?title=Operators_Available_for_Filters_and_Queries#gsc.tab=0

I don't really see why this works the way it does.   ^NQ should just be the same as != by my understanding, which isn't at all what I'm going for here, but I can assure you the query works as I intended it, with ^NQ essentially functioning as a grouped OR.   Has anyone ever run into this, or does anyone know what I might be missing here?

Best regards,

Brian

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Hi Brian, (nice name, btw)



I believe (^NQ) indicates a "new query", which like you said operates like a grouped-OR... looking for a match in either the query conditions preceding it OR the ones that follow.   And I'm pretty sure you could get away with just replacing the second "OR" in your query logic:



      i.e.,



(Caller is (insert js function here)) ^OR (Opened by is (insert js function here)) ^NQ (Company is not empty ^ Company name is not (insert company name here))



Obviously, I've just put in the important parts for demonstration.   Please don't use this at home.   I've seen the documentation you mentioned which relegates '^NQ' to be essentially the same as '!='... and I agree that's more than a little confusing.   Take a look here for some better explanation:



ServiceNow Pro Tips — Understanding Dynamic Filters & Checking a Record Against a Filter Using Glide...



Hopefully that explanation helps a little bit.




Thanks,


-Brian


View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

This might help you understand why all three are needed and what they do differently.


As far as I know, you need to use addEncodedQuery() to query with a grouped OR. I have given an example in the link below.



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



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

Brian Dailey1
Kilo Sage

Hi Brian, (nice name, btw)



I believe (^NQ) indicates a "new query", which like you said operates like a grouped-OR... looking for a match in either the query conditions preceding it OR the ones that follow.   And I'm pretty sure you could get away with just replacing the second "OR" in your query logic:



      i.e.,



(Caller is (insert js function here)) ^OR (Opened by is (insert js function here)) ^NQ (Company is not empty ^ Company name is not (insert company name here))



Obviously, I've just put in the important parts for demonstration.   Please don't use this at home.   I've seen the documentation you mentioned which relegates '^NQ' to be essentially the same as '!='... and I agree that's more than a little confusing.   Take a look here for some better explanation:



ServiceNow Pro Tips — Understanding Dynamic Filters & Checking a Record Against a Filter Using Glide...



Hopefully that explanation helps a little bit.




Thanks,


-Brian


Thank you very much to both of you, I have a more solid understanding now of why ^NQ is essentially signifying a new query, and not a !=



Best regards,


Brian


Deepak Ramar1
Tera Expert

Hi Brian,

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