
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2024 06:38 PM
Can you use both gr.addQuery() and gr.AddEncodedQuery() together in a single database query operation?
Does it matter if you call addQuery() before AddEncodedQuery()?
If you use AddQuery() and THEN addEncodedQuery() will you get normal, expected results, or will the resulting compound query have a BUG in it?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2024 06:41 PM - edited 02-17-2024 06:42 PM
Question 1
Can you use both gr.addQuery() and gr.AddEncodedQuery() together in a single database query operation?
Answer 1
Yes.
Question 2
Does it matter if you call addQuery() before AddEncodedQuery()?
Answer 2
Yes, it matters.
Demonstration
Fire up a "Vancouver" Personal Developer Instance, go to the incident table, and build a compound query such as the following:
Right click the "breadcrumbs" and copy the query to your clipboard. This is your "Encoded Query". Here is what it will look like:
priority=5^sys_updated_by=admin^NQpriority=3^state=7
Now think of a simple, "Regular" (non-encoded) query too add to the mix, such as
category = inquiry
Use "Scripts - Background" to test the order by which you add the queries to a GlideRecord object. Here is some script which does that:
//Case 1. Add Regular Query First.
var gr1= new GlideRecord("incident");
gr1.addQuery("category", "inquiry");
gr1.addEncodedQuery(myEncodedQuery);
gs.addInfoMessage("FinalQuery1: " + gr1.getEncodedQuery());
gr1.query();
gs.addInfoMessage("Row Count: " + gr1.getRowCount());
gs.addInfoMessage("");
//Case 2. Add Encoded Query First.
var gr2= new GlideRecord("incident");
gr2.addEncodedQuery(myEncodedQuery);
gr2.addQuery("category", "inquiry");
gs.addInfoMessage("FinalQuery2: " + gr2.getEncodedQuery());
gr2.query();
gs.addInfoMessage("Row Count: " + gr2.getRowCount());
Observation 1
You get a different "Final Query" and different results (different row count) depending on which function you call first, addQuery() or addEncodedQuery().
Observation 2
If you want the most "restrictive" result, that is, you want the Regular query conditions to apply to ALL the clauses of the Encoded Query, you should use addEncodedQuery FIRST, and then use addQuery().
Question 3
If you use AddQuery() and THEN addEncodedQuery() will you get normal, expected results, or will the resulting compound query have a BUG in it?
Answer 3
Unknown. Who can give a definitive answer to this? Is the addQuery() first behavior "As Designed" or is it buggy? Thanks.
(Please mark as helpful if this article WAS in fact helpful. Thanks.)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2024 06:41 PM - edited 02-17-2024 06:42 PM
Question 1
Can you use both gr.addQuery() and gr.AddEncodedQuery() together in a single database query operation?
Answer 1
Yes.
Question 2
Does it matter if you call addQuery() before AddEncodedQuery()?
Answer 2
Yes, it matters.
Demonstration
Fire up a "Vancouver" Personal Developer Instance, go to the incident table, and build a compound query such as the following:
Right click the "breadcrumbs" and copy the query to your clipboard. This is your "Encoded Query". Here is what it will look like:
priority=5^sys_updated_by=admin^NQpriority=3^state=7
Now think of a simple, "Regular" (non-encoded) query too add to the mix, such as
category = inquiry
Use "Scripts - Background" to test the order by which you add the queries to a GlideRecord object. Here is some script which does that:
//Case 1. Add Regular Query First.
var gr1= new GlideRecord("incident");
gr1.addQuery("category", "inquiry");
gr1.addEncodedQuery(myEncodedQuery);
gs.addInfoMessage("FinalQuery1: " + gr1.getEncodedQuery());
gr1.query();
gs.addInfoMessage("Row Count: " + gr1.getRowCount());
gs.addInfoMessage("");
//Case 2. Add Encoded Query First.
var gr2= new GlideRecord("incident");
gr2.addEncodedQuery(myEncodedQuery);
gr2.addQuery("category", "inquiry");
gs.addInfoMessage("FinalQuery2: " + gr2.getEncodedQuery());
gr2.query();
gs.addInfoMessage("Row Count: " + gr2.getRowCount());
Observation 1
You get a different "Final Query" and different results (different row count) depending on which function you call first, addQuery() or addEncodedQuery().
Observation 2
If you want the most "restrictive" result, that is, you want the Regular query conditions to apply to ALL the clauses of the Encoded Query, you should use addEncodedQuery FIRST, and then use addQuery().
Question 3
If you use AddQuery() and THEN addEncodedQuery() will you get normal, expected results, or will the resulting compound query have a BUG in it?
Answer 3
Unknown. Who can give a definitive answer to this? Is the addQuery() first behavior "As Designed" or is it buggy? Thanks.
(Please mark as helpful if this article WAS in fact helpful. Thanks.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:14 AM
The reason why you're encountering this issue is because you have an ^NQ in your Encoded Query. As you use the different functions for creating your GlideRecord query (addQuery, addOrCondition, addEncodedQuery being the most commonly used), ServiceNow is more or less building a query string that applies to the table. This doesn't work the same way that a filter does when you query on a table using the UI, because you're adding those conditions within the context of one another and then telling ServiceNow to run the resulting query. When you build your query piece by piece in a script, you need to be much more explicit in how you're defining your query. For example, if you wanted to keep using an encoded query for your example, you'd need to write it as follows.
var myEncodedQuery = 'priority=5^sys_updated_by=admin^NQpriority=3^state=7^EQ';
// Your original script
var gr1= new GlideRecord("incident");
gr1.addQuery("category", "inquiry");
gr1.addEncodedQuery(myEncodedQuery);
gs.addInfoMessage("FinalQuery1: " + gr1.getEncodedQuery());
// Updated Script That Still Uses Both Functions
var gr2= new GlideRecord("incident");
gr2.addQuery("category", "inquiry");
gr2.addEncodedQuery(myEncodedQuery);
gr2.addQuery("category","inquiry");
gs.addInfoMessage("FinalQuery2: " + gr2.getEncodedQuery());
// returns category=inquiry^priority=5^sys_updated_by=admin^NQpriority=3^state=7^EQ^category=inquiry
// Explicit Script
var explicitEncodedQuery = "category=inquiry^priority=5^sys_updated_by=admin^NQcategory=inquiry^priority=3^state=7";
var gr3= new GlideRecord("incident");
gr3.addEncodedQuery(explicitEncodedQuery);
gs.addInfoMessage("FinalQuery3: " + gr3.getEncodedQuery()); // returns category=inquiry^priority=5^sys_updated_by=admin^NQcategory=inquiry^priority=3^state=7^EQ
The result of your original script results in the category=inquiry only applying to the first half of your ^NQ, because that's what you told ServiceNow to do! You're building the query piece by piece, and since you told ServiceNow to add the filter of category=inquiry and then to add your Encoded Query after, ServiceNow doesn't have the ability to interpret your instructions as adding the category=inquiry condition to both parts of your Encoded Query since, to ServiceNow, it's just a string. If you run the same addQuery after you add your Encoded Query, you'll get the result you desire.
Just my two cents, but I try to avoid mixing addQuery and addEncodedQuery whenever possible, except for maybe when my addEncodedQuery is something like a Date filter or Dynamic filter which are more complex. In general, I'd try to do something like my third script where everything is in your Encoded Query.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 05:56 AM
Thanks @Alex Winkley . Have you seen any Documentation from ServiceNow along the lines of what you are saying? Anything on mixing the two functions and what the expected behavior is?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 08:09 AM
No problem @G24 - honestly, not really outside the API documentation for GlideRecord, and overall experience with the platform. I'm sure you've already hit the documentation since it seems like you have experience with the platform, but if you haven't, please see here.