JennyHu
Tera Guru

Intended Audience:  Developers who write scripts

Estimated Reading Time:  2-3 minutes

The hype of GlideQuery gets me curious.  What value does it bring over GlideRecord?  After watching the K20 breakout session, GlideQuery: A modern upgrade to GlideRecord by Peter Bell, I was impressed.

Yes, I'd love the system to tell me that I had an error in my script!  Lately, I had to debug a malfunctioned Business Rule that enforces unique number on record creation.  I looked at the script logic.  It was simple and made sense.  I copied it from the ServiceNow doc site.  I couldn't figure out why current.number never got set to the newNum even though it went through the if statement ALL the time.  After troubleshooting by logging the gr.getRowCount() return value, I was surprised that it returned a very high number in the hundreds.  This can't be right.  How could there be hundreds of duplicate numbers.  It turned out that I used the wrong field name, number.  This particular table is the sys_atf_test table, where out-of-the-box there is no default number field.  So we added a custom u_number field.  As humans are creatures of habit, I used the field name number, and rightfully glanced over double checking the field name reference when I was debugging the script.

var curNum = current.number + '';
if (curNum) {
    var recordClass = current.getRecordClassName();
    var gr = new GlideRecord(recordClass);
    gr.addQuery('number', curNum);
    gr.query();
    if (gr.getRowCount() > 0) {
        var newNum = getNextObjNumberPadded();
        gs.addInfoMessage("The number " + curNum + " was already used by another " +
        recordClass + ". The " + recordClass + " number has been changed to " + newNum);
        current.number = newNum;
    }
}

 

I put GlideQuery to the test, to see how this could help me speed up my troubleshooting.  Would it tell me when I'm referencing a field name that doesn't exist in my table?  GlideRecord certainly didn't.  The query went through as if everything was fine and dandy.  In fact, it skipped the invalid query filter and returned all records from the table.

As GlideQuery is not yet available in Orlando, I followed @jacebenson's helpful article on how to install this plugin on my PDI.  A quick test using Xplore and I could see GlideQuery prompted me with the error about the unknown field name.  It also told me the available fields I could reference.  This is excellent.

Hoping this API will be made available in Paris and make developers' lives more enjoyable as Peter puts it. Thank you Peter for developing this API that started off as a side project.  This is great work!

find_real_file.png

Comments
Allen Andreas
Tera Patron

Nice article. We'll see how GlideQuery comes along and how it gets used, but yea, any time I work with tables I would recommend to always check what fields are available especially for tables not extended from task (as one example) and to always check things like rowCount before proceeding to set values and update(), etc.

JennyHu
Tera Guru

Thanks Allen! Good tips.  I'm hoping the table-aware intelligence will one day be made available in NOW's VS Code extension. @Shuvjit, is this feature on the roadmap? 

Shuvjit1
ServiceNow Employee
ServiceNow Employee

@Jenny Hu  Thanks for sharing this. I will review the above and update my comments here.

Version history
Last update:
‎06-05-2020 07:21 PM
Updated by: