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
yesterday
Hi I want to know 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
3 hours ago
Hi @dalvipranit,
Both approaches get you to the same place, they filter records in a GlideRecord query but they’re just different ways of expressing the conditions.
addQuery() / addOrCondition()
- Programmatic and more readable when you’re building queries dynamically.
- Great if you want to add conditions one at a time, or when logic depends on variables/branches.
- Example:
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.addQuery('state', '!=', 7);
gr.query();
Encoded queries (addEncodedQuery())
- Compact “sysparm_query” string form, same syntax you see in filter breadcrumbs.
- Handy when you already know the exact query (copy/paste from a filter).
- Can get messy to read/debug if you’re building it dynamically.
- Example:
var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^state!=7');
gr.query();
When to use which?
- Use addQuery() when building conditions dynamically in code.
- Use encoded queries when you have a static query string (e.g., grabbed from a filter) or need to replicate complex filters quickly.
Both are valid , it really comes down to readability vs. convenience.
Thanks & Regards,
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
3 hours ago - last edited 3 hours ago
Hello @dalvipranit,
Use GlideRecord.addQuery()
for simple, single-field conditions where the field and value are straightforward to construct within the script, offering readability and ease of use. In contrast, use GlideRecord.addEncodedQuery()
for complex queries with multiple conditions, logical operators (AND/OR), or when you need to reuse a pre-defined query string, providing greater flexibility and allowing for quick copy-pasting of encoded queries from the platform's list filter.
When to use addQuery()
-
Simple, single conditions:Best for filtering records based on one field and value, like
current.addQuery('active', true)
. -
Intuitive for new scripts:If you're building a query from scratch,
addQuery()
is generally easier to understand and write for basic filtering.
addEncodedQuery()
-
Complex conditions:Ideal for combining multiple filters using
AND
andOR
logical operators within a single string, such asactive=true^state!=7
. -
Reusing existing filters:You can easily copy the query string generated by the platform's list filter and paste it directly into an
addEncodedQuery()
call in your script. -
Dynamic queries:When the filtering logic is complex and might change, constructing the query as an encoded string outside the script can make it more maintainable.
-
Simplicity for complex scenarios:Instead of writing a long series of
addQuery()
andaddOrCondition()
calls,addEncodedQuery()
allows you to create a concise, single string that represents the entire query.Example: addQuery().var gr = new GlideRecord('incident'); gr.addQuery('active', true); // Add a simple condition gr.query();
Example: addEncodedQuery().
var gr = new GlideRecord('incident'); gr.addEncodedQuery('active=true^state=1^priority=1'); // Multiple conditions in one string gr.query();
If this helped to answer your query, please mark it helpful & accept the solution.Thanks
Santosh.P