If you are new to ServiceNow development, the GlideRecord script is an essential tool to master. GlideRecord scripts allow you to interact with the database and retrieve, insert, or modify records in ServiceNow. Here are the basic steps to get started with GlideRecord scripts:

Step 1: Understand the Basics Before you start writing GlideRecord scripts, it's important to have a basic understanding of JavaScript and the ServiceNow data model. You should also have access to a ServiceNow instance to practice on.

Step 2: Create a New Script To start a GlideRecord script, you first need to create a new script include or business rule. In the script, you can specify which table or tables you want to interact with.

Step 3: Define the GlideRecord Object Next, you need to define a GlideRecord object. This object represents a table in the ServiceNow database and is used to interact with the table's records.

Here are the six most common GlideRecord functions and examples of how they are used:

  1. addQuery() - The addQuery() function allows you to add a query condition to your GlideRecord object. You can use this function to retrieve specific records from the table.

Example:

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('priority', '1');
incidentGR.query();

In this example, the GlideRecord object is created for the incident table, and the addQuery() function is used to add a condition where the priority field equals 1. The query() function is then called to execute the query and retrieve the matching records.

  1. addEncodedQuery() - The addEncodedQuery() function allows you to add a query encoded as a string to your GlideRecord object. This is useful when you want to construct a complex query using multiple conditions. Here's an example:
var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^ORpriority=2');
gr.query();
while (gr.next()) {
  // do something with the records
}
In this example, the addEncodedQuery() function is used to construct a query that returns incidents with a priority of either 1 or 2. The query() function is then called to execute the query, and the while loop is used to iterate through the result set.
💡 Pro Tip: If you can use the filters on a list page in ServiceNow to filter the correct data that you want to see, you can right click on the breadcrumbs link on the top left of the page and you will get an option to "Copy query". This will copy the encoded query to your clipboard that is needed in the script above. This should help you speed up your development time so you don't have to learn what all of the symbols mean.
  1. getValue() - The getValue() function retrieves the value of a field in the current GlideRecord object.

Example:

var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
var shortDesc = incidentGR.getValue('short_description');

In this example, the GlideRecord object is created for the incident table, and the get() function is used to retrieve the record with the number INC0010001. The getValue() function is then used to retrieve the value of the short_description field for that record.

  1. setValue() - The setValue() function allows you to set the value of a field in the current GlideRecord object.

Example:

var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
incidentGR.setValue('state', '2');
incidentGR.update();

In this example, the GlideRecord object is created for the incident table, and the get() function is used to retrieve the record with the number INC0010001. The setValue() function is then used to set the state field to 2, indicating that the incident is in progress. The update() function is then called to save the changes to the record.

  1. insert() - The insert() function allows you to insert a new record into the current GlideRecord object.

Example:

var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
incidentGR.setValue('short_description', 'New Incident');
incidentGR.insert();

In this example, the GlideRecord object is created for the incident table, and the initialize() function is used to create a new, empty record. The setValue() function is then used to set the short_description field, and the insert() function is called to save the new record to the database.

  1. deleteRecord() - The deleteRecord() function allows you to delete the current record from the GlideRecord object.

Example:

var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
incidentGR.deleteRecord();

In this example, the GlideRecord object is created for the incident table, and we use the get() function to lookup the exact record that we want in this situation, then use the deleteRecord() function to delete this incident from the table.

These are just a few of the most common GlideRecord functions. By learning how to use these functions effectively, you can create powerful scripts that automate processes, simplify workflows, and make your ServiceNow instance more efficient.