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
Mega 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

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