Querying with GlideQuery

I’ve been using GlideRecord for the past nine years. The time has come to move on from GlideRecord and experiment with GlideQuery. This post will show you how to run a query using GlideQuery.

Let us get started.

I’d like to run a query that returns all incident numbers with a high priority.


How can I do this with GlideRecord?


We can begin by launching our GlideRecord Object.

var gr = new GlideRecord('incident')


Then add a condition to return only P1 incidents. 

gr.addQuery('priority',1)

Then execute query

gr.query()


Now you have GlideRecord, which contains P1 incidents, and you can loop through the object to get P1 incidents values.

while(myObj.next()){  }


Now let’s use GlideQuery.What I like about GlideQuery is that it is similar to SQL in SQL; for example, if I want to query a P1 incident from a table called incident, I would write something like this.

Select number //I want to select which incident number
from incident // table name
where priority =1 // condition

Now, let’s use GlideQuery. I’ll start GlideQuery and pass the table name.

new GlideQuery('incident') 


Then I need to add a condition to query only P1 incidents.

.where('priority', 1)


Then, I need to specify which columns I need from that table.

.select( 'number')


Finally, I have arrays of incidents. I need to run a function on each element of an array.

.forEach(function (incident) {
});


Here is how code looks like
new GlideQuery('incident') 
.where('priority', 1)
.select( 'number')
.forEach(function (incident) {

gs.info(incident.number);
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s