- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 09:29 AM
I have a GlideRecord query that looks something like this:
var ka = new GlideRecord('x_81991_knowledge');
ka.addQuery('workflow_state', 'Published');
ka.addQuery('kb_category.label', 'Benefits');
var qry = "seriesLIKE"+occ+"^NQlocationLIKE"+loc+"^NQseriesISEMPTY^locationISEMPTY";
ka.addEncodedQuery(qry);
I've noticed that the Encoded query and the regular addQuery dont' seem to work together. For instance in the above query, I would expect the list of knowledge articles to be filtered to only Published articles, and with category Benefits, and then by series/location in the encoded query. However, it seems like the encoded query doesn't take into account the other addquery. This still returns article regardless of category...
Any suggestions?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 10:44 AM
Thanks Patrick and sherrysullivan, I actually got the query to work by simply moving the addEncodedQuery line above all of my addQuery lines. I'm not sure why the order matters, but that solved the issue. If you have any insights as to why this worked, I would love to learn about it. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 09:56 AM
If you remove the addEncodedQuery do you get the expected results? It could be the string you are using for your encoded query; yours looks like a concatenated string where it should just be one string using OR or And to separate the conditions. The best way to grab the query is to go the list view, build a condition, and then right click the breadcrumbs to select the Copy query option. Here's the example from the SN docs:
var queryString = "priority=1^ORpriority=2";
var gr = new GlideRecord('incident');
gr.addEncodedQuery(queryString);
gr.query();
while (gr.next()) {
gs.addInfoMessage(gr.number);
}
https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=r_GlideRecord-AddEncodedQuery_String

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 10:00 AM
David,
You can definitely use addQuery and addEncodedQuery at the same time. Run this in your background scripts and you can see it work:
var incident = new GlideRecord('incident');
incident.addQuery('state', '7');
incident.addEncodedQuery('state=6');
gs.info(incident.getEncodedQuery());
That said, I think you should rework this query and take advantage of condition chaining and addNullQuery to make it more readable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 10:44 AM
Thanks Patrick and sherrysullivan, I actually got the query to work by simply moving the addEncodedQuery line above all of my addQuery lines. I'm not sure why the order matters, but that solved the issue. If you have any insights as to why this worked, I would love to learn about it. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 07:40 AM
Almost 7 years later, I came across this post and wanted to provide some insight into what may or may not be happening here (given I don't know your table well as well as any customizations that happened). I took your code snippet and created two versions, one where the Encoded Query is added first, and one where it's added second.
// EncodedQuery added first
var kb = new GlideRecord('kb_knowledge');
var occ = 'occ';
var loc = 'loc';
var qry = "seriesLIKE"+occ+"^NQlocationLIKE"+loc+"^NQseriesISEMPTY^locationISEMPTY";
kb.addEncodedQuery(qry2);
kb.addQuery('workflow_state', 'Published');
kb.addQuery('kb_category.label', 'Benefits');
return kb.getEncodedQuery(); // returns seriesCONTAINSocc^NQlocationCONTAINSloc^NQseriesISEMPTY^locationISEMPTY^EQ^workflow_state=Published^kb_category.label=Benefits
var kb = new GlideRecord('kb_knowledge');
var occ = 'occ';
var loc = 'loc';
kb.addQuery('workflow_state', 'Published');
kb.addQuery('kb_category.label', 'Benefits');
var qry = "seriesLIKE"+occ+"^NQlocationLIKE"+loc+"^NQseriesISEMPTY^locationISEMPTY";
kb.addEncodedQuery(qry);
return kb.getEncodedQuery(); // returns workflow_state=Published^kb_category.label=Benefits^seriesCONTAINSocc^NQlocationCONTAINSloc^NQseriesISEMPTY^locationISEMPTY^EQ
The big thing to draw from this is that the Encoded Query is added directly onto the tail end of your query, which is where I think you're running into some unintended consequences of how you've formatted your query.
In ServiceNow, there are two ways to add an OR Condition to your query:
- ^OR - I refer to as "little or"
- ^NQ - I refer to as "big or", because it has bigger consequences.
When you use ^OR, the additional condition is only applied to the current condition it follows. So, using Incident as an example for querying, consider the following requirement: "I want to see all Incidents in a State of New AND Priority is 1 - Critical OR 2 - High." Using ^OR, the resulting query would be "state=1^priority=1^ORpriority=2", and return the following data set:
- State is New AND (Priority is 1 - Critical OR Priority is 2 - High)
When you use ^NQ, though, the additional condition can be though of as a completely separate dataset from the first condition. So, using the same requirement, if we replaced ^OR with ^NQ, it would be "state=1^priority=1^NQpriority=2". The result of this, though, is it would return the following two datasets joined together:
- State is New AND Priority is 1 - Critical
- Priority is 2 - High
As a result, the State filter isn't applied to the Priority 2 - High records, meaning that we can't (nor should we) use^NQ here. ^NQ is best used for joining two completely different sets of data, or carefully for when you need to iterate through all permutations of a set of conditions.
For example, consider the following requirement instead: "I want to see all Incidents in a State of New AND Priority is 1 - Critical OR State of Awaiting Problem AND Priority is 2 - High". Using ^NQ to generate this Encoded Query, you would get "state=1^priority=1^NQstate=3^priority=2", which would return the following two datasets joined together:
- State is New AND Priority is 1 - Critical
- State is Awaiting Problem AND Priority is 2 - High
This is good and exactly what we want! If we were to using ^OR instead though, the resulting query would be "state=1^priority=1^ORstate=3^priority=2", which translates to...
- State is New AND (Priority is 1 - Critical OR State is Awaiting Problem) AND Priority is 2 - High
Obviously this isn't what we want, so ^NQ would be the better choice here. So the question is, how do I know when I should use each? Honestly, my best advice is to use the filter on a table to build your query piece by piece until you get the data set back that you want, and use the "Copy Query" functionality by right-clicking on the end of your filter, and using the query that's copied as your structure for building your GlideRecord. In any case, you can always check your GlideRecord's query by putting a log statement and calling the getEncodedQuery() function to doublecheck your query is running as intended.