Hi Folks,
In this video, we will be looking at the undocumented GlideRecord API which is applyEncodedQuery. This is the hidden API applyEncoded query which in my sense is a really powerful API.
First of all, let’s try to understand what is Glide record API, then we will jump into ApplyEncodedQuery.

GlideRecord API :
The GlideRecord API is the primary means of interfacing with the database on the server-side code. A GlideRecord is an object that contains records from a single table. Use the API to instantiate a GlideRecord object and add query parameters, filters, limits, and order.

See the GlideRecord article for details on building and running queries.

For information on a class that performs the same functions as GlideRecord and enforces ACLs, see Using GlideRecordSecure.

Always test queries on a sub-production instance prior to deploying them on a production instance. An incorrectly constructed encoded query, such as including an invalid field name, produces an invalid query. When the invalid query is run, the invalid part of the query condition is dropped, and the results are based on the valid part of the query, which may return all records from the table. Using an insert(), update(), deleteRecord(), or deleteMultiple() method on bad query results can result in data loss.

You can set the glide.invalid_query.returns_no_rows system property to true to have queries with invalid encoded queries return no records.

Now let’s look into applyEncodedQuery,

applyEncodedQuery API :
There are many hidden gems in ServiceNow which are undocumented for some reason or the other. One such thing is an interesting function that I found while looking into the script includes ‘StdChangeUtilsSNC’, called applyEncodedQuery() of GlideRecord.

Let’s just say you have a use case where you have an encoded query and want to create or update a record using it. Here comes applyEncodedQuery() to your rescue. Below is a quick sample on how you can use the function.

addEncodedQuery adds the conditions before querying for results.



var query = “category=Hardware^priority=1”;
var gr = new GlideRecord(“incident”);
gr.addEncodedQuery(query);
gr.query();


applyEncodedQuery takes the values from the query and applies them to a record.


var query = “category=Hardware^priority=1”;
gr = new GlideRecord(‘incident’);
gr.applyEncodedQuery(query);
gr.insert();
//incident created with values from encoded query

Please follow subscribe to my channel Technomonk and press the bell icon to get the latest update on my new videos.
Till then stay happy and safe.
Have a nice day.

Regards,
Amit Gujarathi

technomonkadmin

View all posts

Add comment

Your email address will not be published. Required fields are marked *