Using GlideRecord to query tables

In order to query a table, first create an object for the table.

This object is called a GlideRecord. To create a GlideRecord, create the following in script:
var target = new GlideRecord('incident');

This creates a variable called target which is a GlideRecord object for the incident table. The only parameter needed is the name of the table to be accessed.

To process all records from the incident table, add the following script:
target.query(); // Issue the query to the database to get all records 
while (target.next()) { 
  // add code here to process the incident record 
}

This issues the query() to the database. Each call to next() would load the next record which you would process and do whatever you want to do.

But that is not the common case. Most of the time you actually want to retrieve a specific record or a specific set of records, and you have some criteria (query conditions) that define the records you want to obtain. For example, say you want to obtain all the incident records that have a priority value of 1. Here is the code that would accomplish that.
var target = new GlideRecord('incident'); 
target.addQuery('priority',1);
target.query(); // Issue the query to the database to get relevant records 
while (target.next()) { 
  // add code here to process the incident record 
}
Notice in the above code we added the line target.addQuery('priority', 1);. This is indicating that you only want the records where the priority field is equal to 1. We assume that the majority of queries that you will want to do will be equality queries, queries where you want to find records where a field is equal to a value. Therefore we provide this format of the query and do not make you specify that you want an equals operation, we just assume it. However, lets say you wanted to find all incidents where the priority field is GREATER THAN 1. In that case you have to provide us with the operator that you want to apply to priority and this is done by providing the operator in the addQuery() request as is shown below.
var target = new GlideRecord('incident') ; 
target.addQuery('priority','>',1);
target.query(); // Issue the query to the database to get relevant records 
while (target.next()) { 
  // add code here to process the incident record 
}