- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Not getting results as expected using applyEncodedQuery(),
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Please update the question above, not everyone will see this reply as it is hide between so many replies.
Okay, so you wanted to know about applyEncodedQuery:
applyEncodedQuery() is not for filtering results like addEncodedQuery().
Instead, it sets field values on the current record based on an encoded query string.
This is usually useful when you want to clone field conditions into a new record before insert/update.
Tried this on PDI -
// Step 1: Build an encoded query from an existing GlideRecord
// Here, we search for all Incidents where the description = "test"
var gr1 = new GlideRecord('incident');
gr1.addEncodedQuery('description=test');
gr1.query();
// Save the encoded query string so it can be reused later
// Example of encoded query generated → "description=test"
var encQuery = gr1.getEncodedQuery();
// Step 2: Create a NEW Incident record
var gr2 = new GlideRecord('incident');
gr2.initialize(); // Prepares a blank new record
// Apply the encoded query values we built earlier (in this case: description=test)
// Note: applyEncodedQuery() sets field values based on the query string
gr2.applyEncodedQuery(encQuery);
// Step 3: Insert the new record into the Incident table
var newSysId = gr2.insert();
gs.info("✅ New Incident created with Sys ID: " + newSysId);
// Step 4: Double-check what got created
if (newSysId) {
var check = new GlideRecord('incident');
if (check.get(newSysId)) {
gs.info("ℹ️ New Incident Details → Number: " + check.number +
" | Description: " + check.description );
}
}
What we did in the code
Created a query first
We searched the Incident table for all records where the description = "test".
This gave us an encoded query string (basically, a compact way of saying "description equals test").
Saved that query for reuse
Instead of hard-coding the condition again, we stored the encoded query in a variable.
Example: encQuery = "description=test". (var encQuery = gr1.getEncodedQuery();)
Created a new Incident record
We initialized a blank Incident record.
Added some custom values like short_description and caller_id.
Applied the saved query values to the new record
Using applyEncodedQuery(encQuery), we told ServiceNow:
"Apply all field values from that query (description = test) onto this new record".
Inserted the new record
Finally, we saved the new Incident into the database.
When we checked it, the new record had description = test (inherited from the query) along with our manually added fields.
Note: applyEncodedQuery() is used to take an encoded query string (like "priority=1^active=true") and directly apply those field values to the current GlideRecord object.
Screenshot from Official Servicenow Doc -
Hope you now understand the use case of applyEncodedQuery(). If you still have any doubts, please feel free to ask!
CC : @Shruti D , @Bhuvan , @RaghavSh , @Juhi Poddar , @Ankur Bawiskar , @Nikhil Bajaj9 , @Ravish , @Jennifer Rahman
Please verify — this is what I know to the best of my knowledge. Feel free to add any points if I may have missed something, or correct me if I’m wrong anywhere.
Shashank Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Little Clarification - i.e applyEncodedQuery not addEncoded....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hello @yashkamde,
addEncodedQuery(): This is the standard method for adding encoded queries before executing the query.
applyEncodedQuery(): This is used to filter an existing result set (i.e., after a .query() has already been run).
Your current code with both addQuery('category=Hardware') and applyEncodedQuery('category=Software') will return zero results. You should not combine contradictory conditions (Hardware and Software) unless you're using OR.
Refer the below code:
var gr = new GlideRecord('incident');
// Get incidents where category is Hardware OR Software
gr.addEncodedQuery('category=Hardware^ORcategory=Software');
gr.query();
// Filter only category = Software
gr.applyEncodedQuery('category=Software');
while (gr.next()) {
var c = gr.getDisplayValue('category');
gs.info("Incident: " + gr.number + " | Category: " + c);
}
Please Mark Correct ✔️if this solves your query and also mark Helpful 👍 if you find my response worthy based on the impact.
Regards,
Shruti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
No @Shruti D !
It does not used to filter an existing result set.
See my response in below thread where I explained the use case of it.
Thank you!
Shashank Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Please update the question above, not everyone will see this reply as it is hide between so many replies.
Okay, so you wanted to know about applyEncodedQuery:
applyEncodedQuery() is not for filtering results like addEncodedQuery().
Instead, it sets field values on the current record based on an encoded query string.
This is usually useful when you want to clone field conditions into a new record before insert/update.
Tried this on PDI -
// Step 1: Build an encoded query from an existing GlideRecord
// Here, we search for all Incidents where the description = "test"
var gr1 = new GlideRecord('incident');
gr1.addEncodedQuery('description=test');
gr1.query();
// Save the encoded query string so it can be reused later
// Example of encoded query generated → "description=test"
var encQuery = gr1.getEncodedQuery();
// Step 2: Create a NEW Incident record
var gr2 = new GlideRecord('incident');
gr2.initialize(); // Prepares a blank new record
// Apply the encoded query values we built earlier (in this case: description=test)
// Note: applyEncodedQuery() sets field values based on the query string
gr2.applyEncodedQuery(encQuery);
// Step 3: Insert the new record into the Incident table
var newSysId = gr2.insert();
gs.info("✅ New Incident created with Sys ID: " + newSysId);
// Step 4: Double-check what got created
if (newSysId) {
var check = new GlideRecord('incident');
if (check.get(newSysId)) {
gs.info("ℹ️ New Incident Details → Number: " + check.number +
" | Description: " + check.description );
}
}
What we did in the code
Created a query first
We searched the Incident table for all records where the description = "test".
This gave us an encoded query string (basically, a compact way of saying "description equals test").
Saved that query for reuse
Instead of hard-coding the condition again, we stored the encoded query in a variable.
Example: encQuery = "description=test". (var encQuery = gr1.getEncodedQuery();)
Created a new Incident record
We initialized a blank Incident record.
Added some custom values like short_description and caller_id.
Applied the saved query values to the new record
Using applyEncodedQuery(encQuery), we told ServiceNow:
"Apply all field values from that query (description = test) onto this new record".
Inserted the new record
Finally, we saved the new Incident into the database.
When we checked it, the new record had description = test (inherited from the query) along with our manually added fields.
Note: applyEncodedQuery() is used to take an encoded query string (like "priority=1^active=true") and directly apply those field values to the current GlideRecord object.
Screenshot from Official Servicenow Doc -
Hope you now understand the use case of applyEncodedQuery(). If you still have any doubts, please feel free to ask!
CC : @Shruti D , @Bhuvan , @RaghavSh , @Juhi Poddar , @Ankur Bawiskar , @Nikhil Bajaj9 , @Ravish , @Jennifer Rahman
Please verify — this is what I know to the best of my knowledge. Feel free to add any points if I may have missed something, or correct me if I’m wrong anywhere.
Shashank Jain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Thank you for the clear explanation and guidance! Your breakdown of how the encoded query works and how to apply it dynamically to a new record was really helpful. It clarified exactly what I needed, and the solution worked perfectly in my use case.
Appreciate your support and the time you took to explain it step by step.