addEncodedQuery vs applyEncodedQuery

vikramkehar
Giga Contributor

I often see both addEncodedQuery() and applyEncodedQuery() used in GlideRecord scripts — they seem similar, but are they really doing the same thing? When should I use one over the other, and are there cases where applyEncodedQuery() is actually better or more efficient?

1 ACCEPTED SOLUTION

garimakharb
Giga Guru

addEncodedQuery() adds your condition before the query runs — it filters at the database level.
applyEncodedQuery() filters records after the query is already run — it works in-memory.
Use addEncodedQuery for performance. Use applyEncodedQuery when you already queried and want to re-filter.

View solution in original post

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @vikramkehar

 

Consider this answer 

 

https://www.servicenow.com/community/developer-forum/encoded-query/m-p/1825502

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Manoj89
Giga Sage

Hi Vikram,

 

addEncodedQuery() - can be used just to query records

 

var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true');
inc.setLimit(10);
inc.query();
while (inc.next()) {
    gs.print(inc.sys_id);
}



applyEncodedQuery() - Can be used to set field values on record insert.

var incCreate = new GlideRecord('incident');
incCreate.initialize();
incCreate.applyEncodedQuery('active=true^short_description=test^description=testdescp');
incCreate.insert();



 

garimakharb
Giga Guru

addEncodedQuery() adds your condition before the query runs — it filters at the database level.
applyEncodedQuery() filters records after the query is already run — it works in-memory.
Use addEncodedQuery for performance. Use applyEncodedQuery when you already queried and want to re-filter.