The ServiceNow Nerd

The musings of a ServiceNow platform developer and enthusiast

The SN Nerd

How To Write GlideRecord Queries Like A Pro

by snnerd
Published: Last Updated on 225 views

UTAH+: Try GlideQuery, ServiceNow’s new API for querying data

VANCOUVER+: Try Now Assist for code to write GlideRecord code based on user prompts!

4 Simple Steps To Write GlideRecord Queries Like A Pro

The GlideRecord API is probably the most used Object in any ServiceNow Instance.
It can also be one of the most frustrating!

Code like this is commonplace:

var gr = new GlideRecord('some_table');
var gc = gr.addQuery('field_a','value');
gc.addOrCondition('field_b',;'Complex Query')
gr.addQuery('another_ field', 'more complexity');
gr.addQuery('field_x','furthermore complex');
gr.query();
while (gr.next()) {
//Why am I not getting the record result I want!!!
//Someone please help me!
}

Given how often we need to write this code, it pays off to take the time to develop a methodology to write them faster. 

In this blog, I will show you my method of writing these queries, which I have refined over years of experience developing in the platform.

Example: Catalog Item Variables 

Let’s take querying Catalog Item variables as an example. You’ve identified the table Variable Ownerships [sc_item_option_mtom] and your code looks something like this:

var grVariableOwnerships = new GlideRecord('sc_item_option_mtom'); // Name your variable after the table name. Make it obvious what records you are dealing with
grVariableOwnerships.addQuery('field_a',value);
//Complex query here
grVariableOwnerships.query();
while( grVariableOwnerships.next() {
    processVariableOwnerships(grVariableOwnerships); //Made up function Separate your processing into a separate function for unit testing
}

I have discovered 4 simple steps that will allow you to write your GlideRecord Queries like a Pro and get them right every time! 

  1. Check the Dictionary Entry to confirm Field Names and Labels

    This will ensure you don’t make typos in your field names, and will be useful in step 2.  
  2. Build the Filter in a List

    From Step 1, you will be able to align the Field Labels with the Column Labels. Modify the query until you get the result list you want.  
  3. Copy the Query from the Bread Crumb

    Your query will look something like this:
    sc_item_option.valueISNOTEMPTY^sc_item_option.value!=false^sc_item_option.value!=undefined^request_item.cat_item=0241d1b2db4e4700821a3e5c7c9619b8
  4. Paste it into your code

    var encQry = 'sc_item_option.valueISNOTEMPTY^sc_item_option.value!=false^sc_item_option.value!=undefined^request_item.cat_item=0241d1b2db4e4700821a3e5c7c9619b8'; var grVariableOwnerships = new GlideRecord('sc_item_option_mtom'); // Name your variable after the table name. Make it obvious what records you are dealing with grVariableOwnerships.addEncodedQuery(encQry); grVariableOwnerships.query(); while( grVariableOwnerships.next() { processVariableOwnerships(grVariableOwnerships); //Made up function Separate your processing into a separate function for unit testing }​

    Using the addEncodedQuery API, you can paste your query that you already know works straight into your code! Sometimes, your query values will be based on existing variables. This is easy to integrate into your copied encoded query:

    var value0 = 'some_value'; var value1 = 'another_value'; var value2 = '1234567890'; var encQry = gs.getMessage( 'sc_item_option.valueISNOTEMPTY^sc_item_option.value!={0}^sc_item_option.value!={1}^request_item.cat_item={2}', [value0, value1, value2] );
sc_item_option.valueISNOTEMPTY^sc_item_option.value!=some_value^sc_item_option.value!=another_value^request_item.cat_item=1234567890

Using to getMessage API you can easily incorporate values into your queries and still use the query you know that works.
Feel free to refactor your code back into addQuery if you feel it makes your code more maintainable and readable. You can always use getEncodedQuery to confirm that your refactored query matches the original. Best of all, you can apply this approach to any GlideRecord query problem you have

API Used

No animals were harmed in the making of this blog post.
However, the following API was used/abused:

ClassMethod
GlideRecordaddEncodedQuery
GlideRecordgetEncodedQuery
GlideRecordquery
GlideSystemgetMessage

I hope this blog post was useful!

Related Posts

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More