The Zurich release has arrived! Interested in new features and functionalities? Click here for more

What is the use of addEncodedQuery?

yashkamde
Tera Contributor

Not getting results as expected using applyEncodedQuery(),

var gr = new GlideRecord('incident');
gr.addQuery('category=Hardware');
gr.query();

gr.applyEncodedQuery('category=Software');

while (gr.next()) {
    var c = gr.getDisplayValue('category');
    gs.info("Incident: " + gr.number + " | Category: " + c);
}
1 ACCEPTED SOLUTION

@yashkamde ,

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

  1. 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").

  2. 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();)

  3. Created a new Incident record

    • We initialized a blank Incident record.

    • Added some custom values like short_description and caller_id.

  4. 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".

  5. 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 -

Screenshot (19).png

 

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.

 

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain

View solution in original post

16 REPLIES 16

Juhi Poddar
Kilo Patron

Hello @yashkamde 

If you are trying to add query where the Category is Hardware or Category is Software , the code should look like this:

var gr = new GlideRecord('incident');
gr.addEncodedQuery('category=hardware^ORcategory=software');
gr.query();

while (gr.next()) {
    var category = gr.getDisplayValue('category');
    gs.info("Incident: " + gr.number + " | Category: " + category);
}

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You

Juhi Poddar

Bhuvan
Kilo Patron

@yashkamde 

 

Encoded Query is used when you want to filter the records on complex conditions. You can go to form view, build the filter and copy the query.

 

For example, if you want to query a condition like below, create a filter condition and copy the query and use it in glide record

 

Bhuvan_2-1756139780032.png

Bhuvan_5-1756140147069.png

Bhuvan_6-1756140170436.png

var gr = new GlideRecord('incident');
gr.addEncodedQuery('priorityIN1,2,3,4^categoryINinquiry,software,hardware,network^caller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
gr.query();
while (gr.next())
{
gs.print(gr.number);
}

 

You can create the script without using addEncodedQuery and use one or more addQuery and build the logic and would give same results. 

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan