GlideRecord is a class in ServiceNow and it is used to perform database operations without writing SQL queries.
You can use GlideRecord in ServiceNow to manipulate records in tables. GlideRecord is a powerful API that allows you to perform CRUD (Create, Read, Update, and Delete) operations on ServiceNow tables. You can use GlideRecord to query, insert, update, and delete records in any ServiceNow table.
Here are some common use cases where GlideRecord can be used:
Querying Records: You can use GlideRecord to query records from a table based on certain conditions.
Updating Records: You can use GlideRecord to update the field values of a record in a table.
Creating Records: You can use GlideRecord to create a new record in a table.
Deleting Records: You can use GlideRecord to delete a record from a table.
Here's a basic example of a GlideRecord script.
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('priority', 1);
incidentGR.orderBy('number');
incidentGR.query();
while (incidentGR.next()) {
gs.info(incidentGR.number + ' ' + incidentGR.short_description);
}
Let me explain you each line of this code:
1. var incidentGR = new GlideRecord('incident');
This line is initializing a new GlideRecord object for the 'incident' table. 'incidentGR' is just a variable name - you could name this variable whatever you like. 'incident' is the name of the table in the ServiceNow database that you want to query.
2. incidentGR.addQuery('priority', 1);
This line is adding a query to the GlideRecord object. The 'addQuery' method is used to add a filter to the query. In this case, it's filtering for records where the 'priority' field is 1. This is equivalent to the SQL WHERE clause.
3. incidentGR.orderBy('number');
This line is ordering the results of the query by the 'number' field. This is equivalent to the SQL ORDER BY clause.
4. incidentGR.query();
This line is executing the query. Up until this point, we've only been setting up the query. This line actually sends the query to the database and retrieves the results.
5. while (incidentGR.next())
This line is starting a while loop that will iterate over each record returned by the query.
The 'next()' method moves to the next record in the result set.
6. {
This is opening of while loop.
7. gs.info(incidentGR.number + ' ' + incidentGR.short_description);
This line is logging the 'number' and 'short_description' fields of the current record.
'gs.info' is a method used to write to the ServiceNow system log.
8. }
This line is closing the while loop.
Remember, to use GlideRecord, you must have the appropriate database access permissions in your ServiceNow instance. Also, be careful not to inadvertently modify or delete data when using GlideRecord, especially in a production environment.
You can use GlideRecord in various types of scripts in ServiceNow.
Here are some examples:
Business Rules: You can use GlideRecord in business rules to perform actions on records before or after they are saved to the database.
Client Scripts: You can use GlideRecord in client scripts to manipulate data on the client side, such as setting field values or validating user input.(Read Note Below about using GlideRecord in Client Script)
Script Includes: You can use GlideRecord in script includes to create reusable code that can be called from multiple scripts.
Scheduled Jobs: You can use GlideRecord in scheduled jobs to perform batch operations on records, such as updating or deleting multiple records at once.
UI Policies: You can use GlideRecord in UI policies to control the behavior of fields on a form, such as hiding or disabling fields based on certain conditions.
Scripted REST APIs: You can use GlideRecord in scripted REST APIs to expose ServiceNow data to external systems.
Overall, GlideRecord can be used in any script where you need to interact with ServiceNow records, whether it's on the server side or the client side.
Using GlideRecord in Client Script
While GlideRecord can be used in client scripts to manipulate data on the client side, it is generally not recommended to use it in client-side scripts due to performance and security concerns.
Client scripts are executed on the user's browser, so using GlideRecord in a client script can cause additional load on the server and increase the time it takes for the script to execute. Additionally, because client scripts run on the user's browser, it is possible for malicious users to modify the script to perform unauthorized actions, such as deleting or modifying records they should not have access to.
Instead of using GlideRecord in client scripts, it is recommended to use client-side APIs provided by ServiceNow, such as the GlideForm API or GlideAjax. These APIs are optimized for client-side performance and have built-in security features to prevent unauthorized actions.
If you need to perform complex operations that require server-side processing, you can use AJAX calls to invoke server-side scripts and retrieve the results on the client side. This approach allows you to perform server-side processing while minimizing the impact on the client-side performance and security.
GlideRecord API with practical examples
Example
Query incidents where Short Description and Description contains 'Infrastructure' keyword.
var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'CONTAINS', 'Infrastructure');
gr.addQuery('description', 'CONTAINS', 'Infrastructure');
gr.query();
while (gr.next()) {
gs.print(gr.number + ': ' + gr.short_description);
}
Example
Query incidents where State is Resolved and number of records returned should be 10.
var gr = new GlideRecord('incident');
gr.addQuery('state', 'Resolved');
gr.setLimit(10);
gr.query();
while (gr.next()) {
gs.print(gr.number + ': ' + gr.short_description);
}
Example
Here's an example of how you can update incidents with a priority of 1 while excluding system fields from the update operation using autoSysFields(false) in the GlideRecord API.
var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.query();
gr.autoSysFields(false); // Exclude system fields from the update operation
while (gr.next()) {
gr.setValue('short_description', 'Updated Short Description');
gr.setValue('description', 'Updated Description');
gr.update();
}