GlideRecord sample scripts
The GlideRecord API is used for database operations on the server-side code. A GlideRecord is an object that contains records from a single table. We use the API to create a GlideRecord object and add query parameters, filters, limits, and ordering.
Refer the below operations for more details.
Insert
Insert a new record:
//Create a new Incident record and populate the fields with the values below
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'Issue with network';
gr.category = 'network';
gr.caller_id.setDisplayValue('Abel Tuter');
gr.insert();
(OR)
//Create a new Incident record and populate the fields with the values below
var gr = new GlideRecord('incident');
gr.initialize();
gr.setValue('short_description', 'Issue with network');
gr.setValue('description', 'Facing network issue');
gr.setValue('state', 1);
gr.setValue('category', 'network');
gr.setDisplayValue('caller_id', 'Abel Tuter');
gr.insert();
Update
Updates the GlideRecord with any changes that have been made.
Here, update one or many records by querying the records, setting the values on the records, and then calling .update();
//Find all active incident records and make them inactive
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}
Update Multiple
Updates each GlideRecord in a stated query with a specified set of changes.
Note: To ensure expected results, use the setValue() method instead of direct assignments. That is, use gr.setValue('<field_name>', '4')); instead of gr.<field_name> = 4.
//Find all active incident records and update state value
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.setValue('state', 2);
gr.updateMultiple();
Query
Basic GlideRecord query
var gr = new GlideRecord('incident'); // Indicate the table to query from
gr.addQuery('active', true); // addQuery allows to restrict the query to the field/value.
gr.query(); // Execute the query.
while (gr.next()) { // While the record set contains records, iterate through them.
// Do something with the records returned
if (gr.category == 'software') {
gs.log('Category is ' + gr.category);
}
}
(OR)
var gr = new GlideRecord('incident'); // Create a new GlideRecord object for the 'incident' table
gr.addQuery('active', true); // Add a query condition to filter records where the 'active' field is true.
gr.query(); // Execute the query.
while (gr.next()) { // Loop through each record returned by the query
gs.log('Category is ' + gr.category); // Log the value of the 'category' field for each record.
}
Delete
Delete one or many records by querying for the records and then calling the .deleteRecord() method.
var gr = new GlideRecord('incident'); // Create a new GlideRecord object for the 'incident' table
gr.addQuery('active', false); // Add a query condition to filter records where the 'active' field is false.
gr.query(); // Execute the query.
while (gr.next()) { // Loop through each record returned by the query
gr.deleteRecord(); // Deletes the current record from the GlideRecord object.
}
Delete Multiple
If we are deleting multiple records, then the ‘deleteMultiple’ method can be used as a shortcut.
var gr = new GlideRecord('incident'); // Create a new GlideRecord object for the 'incident' table
gr.addQuery('active', 'false'); // Add a query condition to filter records where the 'active' field is 'false'.
gr.query(); // Execute the query.
gr.deleteMultiple(); // Delete all records that match the query conditions.
Add Encoded Query
Encoded query strings can be copied directly from a filter, by right-clicking on the breadcrumbs.
var gr = new GlideRecord('incident'); // Create a new GlideRecord object for the 'incident' table
gr.addEncodedQuery('active=true^state=2'); // Add an encoded query to filter records where 'active' is true and 'state' is 2
gr.query(); // Execute the query
while (gr.next()) { // Loop through each record returned by the query
gs.log('Category is ' + gr.category); // Log the value of the 'category' field for each record
}
Glide Aggregate
Aggregates include COUNT, SUM, MIN, MAX, AVG
var gr = new GlideAggregate('incident'); // Create a new GlideAggregate object for the 'incident' table
gr.addQuery('active', true); // Add a query condition to filter records where the 'active' field is true
gr.addAggregate('COUNT'); // Add an aggregation function to count records
gr.query(); // Execute the query
var incidents = 0; // Initialize a variable to store the count of incidents
if (gr.next()) { // Check if there are records returned by the query
incidents = gr.getAggregate('COUNT'); // Get the count of incidents using the aggregate function
gs.log('Active incident count: ' + incidents); // Log the count of active incidents
}
Get (sys_id)
Get a single GlideRecord with sys_id. A great way to return a single record when you know the sys_id of that record.
var gr = new GlideRecord('incident');
gr.get('sys_id'); // pass in the sys_id of the record you want.
//Do something with the record returned
if(gr.category == 'hardware'){
gs.log('Category is ' + gr.category);
}
OR Query
The standard ‘addQuery’ parameter acts like an ‘and’ condition in your query. This example shows how you can add ‘or’ conditions to your query.
var gr = new GlideRecord('incident');
gr.addQuery('state', 6);
gr.addOrCondition('state', 7);
gr.query();
while(gr.next()) {
gs.log('Category is ' + gr.category);
}
(OR)
var gr = new GlideRecord('incident');
var orGr = gr.addQuery('state', 6);
orGr.addOrCondition('state', 7);
gr.query();
while(gr.next()) {
gs.log('Category is ' + gr.category);
}
OR (the simple way)
In addition to the example above this, you can also chain your ‘OR’ condition like below, which is usually simpler.
//Find all incidents with a priority of 1 or 2
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1).addOrCondition('priority', 2);
gr.query();
Add Null Query
Used to search for empty values.
//Find all incidents where the Short Description is empty
var gr = new GlideRecord('incident');
gr.addNullQuery('short_description');
gr.query();
Add Not Null Query
Used to search for not empty values.
//Find all incidents where the Short Description is not empty
var gr = new GlideRecord('incident');
gr.addNotNullQuery('short_description');
gr.query();
Get Row Count
Used to get the number of results returned. Not available client-side.
//Log the number of records returned by the query
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
gs.log('Incident count: ' + gr.getRowCount());
‘getRowCount’ isn’t available client-side, however you can return the number of results in a client-side GlideRecord query by using ‘rows.length’ like below:
//Log the number of records returned by the query
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
alert('Incident count: ' + gr.rows.length);
Order By / Order By Desc
Order the results of your recordset by using ‘orderBy’ and/or ‘orderByDesc’ as shown below.
//Find all active incidents and order the results ascending by category then descending by created date
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.orderBy('category');
gr.orderByDesc('sys_created_on');
gr.query();
Set Limit
Used to limit the number of results returned
//Find the last 10 incidents created
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();
List of Operators
These operators can be used in addition to the standard field/value query searching shown above…
// =
//Equals (this is the same as not including the 3rd parameter)
addQuery('priority', '=', 1);
// >
//Greater than
addQuery('priority', '>', 1);
// <
//Less than
addQuery('priority', '<', 1);
// >=
//Greater than or equals
addQuery('priority', '>=', 1);
// <=
//Less than or equals
addQuery('priority', '<=', 1);
// !=
//Not equals
addQuery('priority', '!=', 1);
// STARTSWITH
//Field must start with value
addQuery('short_description', 'STARTSWITH', 'Printer');
// ENDSWITH
//Field must end with value
addQuery('short_description', 'ENDSWITH', 'Printer');
// CONTAINS
//Field must contain value somewhere
addQuery('short_description', 'CONTAINS', 'Printer');
// DOES NOT CONTAIN
//Field must not contain value anywhere
addQuery('short_description', 'DOES NOT CONTAIN', 'Printer');
// IN
//Field must be found somewhere in the value
addQuery('sys_id', 'IN', '57af7aec73d423002728660c4cf6a71c,ed92e8d173d023002728660c4cf6a7bc');
// INSTANCEOF
//Return only records that are instances of an extended table (like incident is of task in this example)
addQuery('sys_class_name', 'INSTANCEOF', 'incident');
Set Workflow
Used to enable or disable the triggering of Business Rules
var gr = new GlideRecord('incident');
gr.addNullQuery('short_description');
gr.query();
while(gr.next()){
gr.short_description = 'Must be the printers that are broken again...';
gr.setWorkflow(false); //Disable business rules for this query
gr.update();
}
Auto Sys Fields
Used to prevent updating fields such as sys_updated_on, sys_updated_by, and other 'sys' fields.
//Change the category of all 'software' incidents to 'hardware' without updating sys fields
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
while(gr.next()){
gr.category = 'hardware';
gr.autoSysFields(false);
gr.update();
}
Set Force Update
Used to force an update, even when no fields are changed, and thus force the update to execute.
var gr = new GlideRecord('incident');
gr.query();
while(gr.next()) {
gr.setForceUpdate(true); // Force an update even with no changes
gr.update();
}