What’s the difference between GlideRecord’s addQuery() vs. encoded queries – when to use which?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2025 04:38 AM
What’s the difference between GlideRecord’s addQuery() vs. encoded queries – when to use which?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 12:04 AM - edited 09-29-2025 12:06 AM
Hello @dalvipranit ,
addQuery() – Programmatic Filtering
A method used in ServiceNow GlideRecord to add individual filter conditions one by one.
Best for: When you want to build queries dynamically using logic.
Example :
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.addQuery('active', true);
gr.query();
addEncodedQuery() – Compact and Reusable
A method used in GlideRecord to add multiple filter conditions in a single string.
Best for: When you already know the full query string or want to copy it from a filter in the list view.
Example :
var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^active=true');
gr.query();
If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 12:06 AM
In ServiceNow, both addQuery() and encoded queries are used with GlideRecord to filter records, and both support dynamic use. The key difference lies in how the query is built: addQuery() offers step-by-step, programmatic construction, making it ideal for scripts where conditions depend on logic or user input. Encoded queries, on the other hand, are compact string-based filters often copied from list views, and are required in certain areas like UI Builder, For example, when building a dynamic incident dashboard in UI Builder, a complex encoded query string may be needed to handle multiple filter conditions that can’t be expressed using addQuery() in that context.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 12:12 AM
There is no real difference between the 2.
addQuery is typically easier to read than addEncodedQuery but the outcome is the same in the end.
Typically I use addQuery when setting up scripts to run several times over time (logic).
addEncodedQuery I use for cleanup jobs typically run once because you can go to a list, filter the exact records you want to work with - right click on the filter and "Copy query". Its easy and fast and because the script only run once its not that important how "readable" is it.
So my 5 cents is that when builing logic - try to keep it as easy as possible to read for other people.
If you need something run once or similar then its not _that_ important how its set up.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 12:13 AM
Hi @dalvipranit ,
addQuery()
-Adds one condition at a time
-Used when building dynamic or readable queries in scripts
gr.addQuery('active', true);
gr.addQuery('priority', 1);
addEncodedQuery()
-Adds multiple conditions at once using a filter string
-Used when copying filters from list view or for quick queries
gr.addEncodedQuery('active=true^priority=1');
If you found my response helpful, please mark it as helpful and accept it as the solution.
Thank you
Nawal Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 11:14 AM
Hi @dalvipranit ,
addQuery() → You build the filter step by step in your script.
Example:
gr.addQuery("state", "1");
gr.addQuery("priority", "2");
Good when conditions are dynamic (decided at runtime).
addEncodedQuery() → You paste in the whole filter string exactly like you see in the list view filter.
Example:
gr.addEncodedQuery("state=1^priority=2");
Good when you already know the exact filter and want it quickly in one line.
👉 Use addQuery() when you’re building conditions programmatically.
👉 Use addEncodedQuery() when you already have the filter string or want a shortcut.
