Objective: Learn how to use GlideRecord
within ServiceNow
The GlideRecord
class is used for executing database operations without having to write SQL queries. GlideRecord
can be useful for retrieving records which would be difficult to find using the GUI filtering options.
To practice using GlideRecord
:
- Open your personal developer instance
- In the navigation menu, search
System Definition
and then select the sub-categoryScripts - Background
- Copy this script into the text area:
// Here is an example that gets the count of new incidents and provides the INC number & link
// create new GlideRecord from incident table
// Filter for active incidents in state 1 (new)
var incident_gr = new GlideRecord('incident');
incident_gr.addQuery('active', true);
incident_gr.addQuery('state', '1');
incident_gr.query();
// Print out number of new incidents
gs.info("Number of new incidents:");
gs.info(incident_gr.getRowCount());
// Loop through record and print INC number and link
gs.info("Printing incident numbers:");
while(incident_gr.next()) {
gs.info(incident_gr.getValue('number'));
gs.info(incident_gr.getLink(false));
}
In the above code:
-
var incident_gr = new GlideRecord('incident');
creates a newGlideRecord
object calledincident_gr
which accesses the incident table. Important: refrain from usinggr
as a name for yourGlideRecord
Objects as many scripts use this name for theirGlideRecord
objects. Having differentGlideRecord
objects with the same name could produce odd errors which are difficult to debug. -
addQuery()
is used to add filters to our query. These filters help specify properties which we would like our queried results to have. -
query()
tells our GlideRecord object to query the records which satisfy the filters we added usingaddQuery()
. All results are added to a list in ourGlideRecord
object. -
gs.info()
serves as a print function, allowing us to display information to the user. -
next()
returns the next record in the list of records contained in our GlideRecord object. It should be noted that when used in the while loop:
while(incident_gr.next()){
//some code
}
The loop evaluates to true
if the next object in the list is not null, or in otherwords, if there are still object(s) in the list.
getValue()
returns a property value of the current record.
- Click the
Run script
button - The script will run and the result will show something like this:
*** Script: Number of new incidents:
*** Script: 1
*** Script: Printing out incident numbers:
*** Script: INC000046
*** Script: incident.do?sys_id=a9e30c7dc61122760116894de7bcc7bd&sysparm_stack=incident_list.do?sysparm_query=active=true
You can use this process to test out different GlideRecord examples
We can use GlideRecord methods to make changes such as inserts, updates, and deletions.
Here's an example to create a new incident using the initialize
and insert
methods.
//Create a new Incident record and populate the fields with the values below
var inc_gr = new GlideRecord('incident');
inc_gr.initialize();
inc_gr.setValue('short_description', 'Issue with signing into email');
inc_gr.setValue('category', 'email');
inc_gr.caller_id.setDisplayValue('Earl Duque');
inc_gr.insert();
For additional information, refer to: Using GlideRecord to Query Tables