What It Does

The addEncodedQuery() method appends an encoded query string to the current GlideRecord's filter conditions. Encoded queries use ServiceNow's internal query language where conditions are structured as field^operator^value and multiple conditions are joined with '^' characters.

Internally, ServiceNow parses the encoded query string and converts it into the same filter conditions you'd create with addQuery() method calls. The platform validates field names against the target table's schema and converts operators into database-appropriate WHERE clauses when the query executes.

Each call to addEncodedQuery() creates an AND relationship with existing query conditions. Multiple encoded queries stack together, and conditions within a single encoded query string use the logical operators embedded in the string (typically AND, but OR is supported with the '^OR' syntax).

The method handles malformed queries by throwing runtime exceptions during query execution, not during the method call itself. Invalid field names or operators cause the query to fail when query() executes, making debugging more difficult than with explicit addQuery() calls.

This method works identically to addQuery() for building filters but accepts pre-formatted query strings instead of individual field/operator/value parameters. Both methods can be mixed in the same GlideRecord, and both respect the same query ordering and AND logic.

When to Use This

Use addEncodedQuery() when you have query strings copied from the ServiceNow UI (filter breadcrumbs or right-click > Copy query in list views). This is the fastest way to replicate complex user-built filters in server-side scripts without manually parsing each condition into separate addQuery() calls.

Prefer addQuery() for new code where you're building queries programmatically. Explicit field/operator/value parameters are more readable, easier to debug, and provide better IDE support. Reserve encoded queries for situations where the query string comes from user input, saved filters, or UI integrations.

Avoid concatenating user input directly into encoded query strings without validation. This creates injection vulnerabilities where malicious users can manipulate query logic. Always validate or escape user-provided values, or use addQuery() with parameterized values instead.

Return Value

The method returns void and modifies the GlideRecord instance in place. Success or failure cannot be determined from the method call itself since query parsing and validation happen during query execution, not during filter setup.

Invalid encoded query strings throw runtime exceptions when query() executes, not when addEncodedQuery() is called. Wrap query execution in try-catch blocks if you're working with dynamic or user-provided encoded queries.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No Business Rules, ACLs, or audit records are created by adding query conditions—these fire only during record retrieval and modification
  • Query parsing happens during execution, not when the encoded query is added, potentially causing delayed error discovery
  • Performance is identical to equivalent addQuery() calls since both compile to the same database WHERE clauses
  • Field-level ACLs are evaluated when the query executes, not when query conditions are defined
  • Multiple encoded queries on the same GlideRecord create AND conditions between each encoded string
  • No difference in behavior between Business Rules, Script Includes, or Scheduled Jobs—all execute with the same query parsing logic
⚠️

Encoded query syntax errors throw exceptions at query execution time, not when addEncodedQuery() is called, making debugging more difficult than explicit addQuery() calls.