CapaJC
ServiceNow Employee
ServiceNow Employee

There are some shortcuts in GlideRecord that aren't commonly used, but can save you a few keystrokes if you write a lot of scripts. Here's an example that uses four of them.



var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.addNullQuery("assigned_to");
gr.addNotNullQuery("assignment_group");
gr.addQuery("category", "hardware").addOrCondition("category", "software");
gr.query();


The first, gr.addActiveQuery(), is the same as doing gr.addQuery("active", true); There is also an addInactiveQuery() method.

The second, gr.addNullQuery("assigned_to"), is the same as doing gr.addQuery("assigned_to", "");

The third, gr.addNotNullQuery("assignment_group"), is the same as doing gr.addQuery("assigment_group", "!=", "");

The last one is a bit different. One way to include an "OR" condition that you might see in scripts is as follows:


var qc1 = gr.addQuery("category", "hardware");
qc1.addOrCondition("category", "software");


This will include records in either category. The qc1 variable ends up being a QueryCondition object, which is not important to know. QueryCondition has the useful method addOrCondition() that lets you do the OR query.

But there's no need for the two lines - you can combine them like in the example above, which actually is more readable (at least to me).