What is addEncodedQuery()
This is something that will end up saving you a lot of time and can make your GlideRecord queries a lot cleaner and shorter.
Being a ServiceNow Admin is all about learning how to manage your scripts and having high output.
ServiceNow’s addEncodedQuery() allows a ServiceNow Admin to use the condition builder, to build out several conditions on a table – and then use those conditions in a script.
So instead of using addQuery() and other methods several times and having a super long query, you can instead just include it all in a single line of code.
This is particularly helpful when building queries around complex login and dates.
If you can build a query using the condition builder in the list view, then you can return those records with a script.
Keep in mind that using addEncodedQuery returns the exact same values as bulding it out a line at a time in a query. addEncodedQuery is just a little faster and acts as shorthand when you have either a really long query to build out or something that is more complex.
To try this out, you’ll want to run a background scrip to return a record. If you haven’t used a background script yet, check out our post here.
If you’re a new ServiceNow Admin, you’ll also want to checkout how to return a single record using the get() method, to speed up your GlideRecord queries.
Why Should You Use addEncodedQuery()
The real answer here is convenience.
You can build out a GlideRecord query with 10 lines of code, or you can simply copy an “encoded query” and have ServiceNow build it out for you.
You essentially are defining a query that is encoded and truncated and using that as your parameter for your method in a GlideRecord.
This will really just save you time and is something that every ServiceNow Admin should have a comfortable knowledge with.
addEncodedQuery can only be used in a server side operation, so think background script or business rule. Don’t use this on the client as it will cause performance issues.
How To Use addEncodedQuery()
Role Required: admin
There are two parts here.
You first need to learn how to build out an encoded query.
Then you need to learn how to use the encoded query.
1) How To Build An Encoded Query
Most people are going to just go to the List View and build it out from there. It’s much faster and you don’t have to remember or really write any complex syntax or code.
So to start, just go to the list view of the table you want to build our your encoded query on.
Once you’re on the list view, you can then start building our your conditions with the condition builder.
To have a proper encoded query, you need to build out a condition that returns records from the table.
So we will start building out an encoded query, to see what it looks like and then how to use it in a script. This is a random group of conditions to show you how it can be used.
We will search the incident table for all P1 and P2, where the Caller is Fred Luddy and the Category is Inquiry/Help. This will boil down to one line of code when it’s encoded, as to 4 different lines in a script.
So run the query to make sure you have at least one record returned.
To finally “get” your encoded query, right click on the green text in the condition and select “Copy query”. You now have the encoded query in your computer’s clipboard.
When you paste the query, it won’t make a ton of sense to you if it’s your first time seeing it.
This is what an encoded query looks like: priority=1^ORpriority=2^caller_id=5137153cc611227c000bbd1bd8cd2005^category=inquiry
2) Now that you have your encoded query, we can use it in a GlideRecord Query
Let’s interact with this record via a script.
Go ahead and pull up a background script. We’ll use a background script here, but you can use this in any server side operation.
Let’s start building out a simple query here, using our encoded query. Both of these queries simply print out the incident number, as an example. But we are proving that the query builds are returning the same exact record. For the longer example, we’ll use a couple addQuery lines with an addOrCondition. If you want to learn more about the addOrCondition method, check out our post here.
Here is what the query would look like, if you didn’t use the encoded query.
The Script:
var inc = new GlideRecord(“incident”);
inc.addQuery(“priority”, “1”).addOrCondition(“priority”, “2”);
inc.addQuery(“caller_id”, “5137153cc611227c000bbd1bd8cd2005”);
inc.addQuery(“category”, “inquiry”);
inc.query();// Only returns 1 record
if (inc.next()) {
gs.print(inc.number);
}
Here is what the query looks like, when you do use the encoded query. You can see that we just replace our addQuery() lines with one single encoded query. This just showcases the benefit and ease of using the encoded query as a shortcut.
The Script:
var inc = new GlideRecord(“incident”);
inc.addEncodedQuery(“priority=1^ORpriority=2^caller_id=5137153cc611227c000bbd1bd8cd2005^category=inquiry”);
inc.query();// Only returns 1 record
if (inc.next()) {
gs.print(inc.number);
}
Both queries actually do the exact same thing, and return the exact same number of records.
Using addEncodedQuery is just a shorthand and is a faster way to return records, especially when building out more complicated conditions. Again, this is super helpful when dealing with more complex queries. Keep in mind, if you can show it in a list view, then you can interact with it directly with any server side script.
[…] How To Use addEncodedQuery() with GlideRecord in ServiceNow […]
[…] Use the addEncodedQuery method to build more complex queries using encoded parameters. This method allows you to specify an encoded query string that can include multiple filters, sorts, and other parameters, making it easier to build complex queries. I use addEncodedQuery() all the time, learn how to build out super quick and efficient addEncodedQuery() methods here. […]