Aircraft by Modelmodel-aircrafts.com

Glide Record Cheat Sheet in ServiceNow

A GlideRecord cheat sheet provides the essential methods for querying, creating, and updating records in ServiceNow using server-side JavaScript. You use it to interact with database tables directly from scripts, bypassing standard UI workflows for performance or automation. This approach is strictly for backend logic like business rules, scripts, and scheduled jobs.

Core Query Methods

The most common task is retrieving data from a table. You start by instantiating the GlideRecord object with the table name. Then you add conditions to narrow down the results before executing the query. The addQuery method is your primary tool for filtering. It accepts two arguments: the field name and the value you are looking for. You can chain multiple calls to this method to combine conditions with an AND operator.

If you need more complex logic, such as OR conditions or wildcards, you should use the addEncodedQuery method instead. This takes a string that follows ServiceNow’s specific query syntax. For example, you might encode a condition to find records where a date is greater than today. After setting your filters, you call query() to run the search. This moves the internal pointer to the first matching record. To iterate through all results, you wrap your processing code in a while (gr.next()) loop.

Common Filter Operators

You often need to check if a field contains specific text or falls within a range. Use equals for exact matches. Use startsWith or endsWith for partial text searches. For numeric fields, use greaterThan, lessThan, or between. Remember that these operators are case-sensitive for string fields unless you explicitly convert the input to lowercase first. Always validate your inputs before passing them into the query to prevent injection issues.

Creating and Updating Records

When you need to insert new data, you initialize a new record instance. Call initialize() on your GlideRecord object. This sets up the default values for the table. Next, assign values to the fields you want to populate. You can set strings, numbers, dates, and references just like you would in any other variable assignment. Once all desired fields are set, call insert(). This saves the record to the database and returns the new sys_id.

Updating existing records follows a similar pattern but requires you to locate the target first. You can find a record by its unique identifier using get(sys_id). This method returns true if the record exists and loads the data into the object. If you find the record, change the field values you wish to modify. Then call update() to save the changes. Unlike insert, update does not return a value; it simply persists the modifications. Always check if the record was found before attempting to update it to avoid errors.

Performance Considerations

Direct database access is powerful but carries risks. Every query consumes system resources. Avoid running queries inside loops whenever possible. Instead, retrieve all needed data in one batch and process it in memory. This reduces the number of database hits significantly. Also, be careful with large result sets. If a query might return thousands of rows, consider adding limits or refining your filters. Unoptimized queries can slow down the entire platform for all users.

Deleting and Managing Data

Removing records is done with the delete() or deleteMultiple() methods. Use delete() when you have a single record loaded in your GlideRecord object. This triggers any configured delete rules or ACLs. Use deleteMultiple() to remove a set of records based on current query filters. This method is faster for bulk operations but may bypass some business logic depending on configuration. Always double-check your filters before calling delete functions to prevent accidental data loss.

You can also manipulate related records using reference fields. When you load a parent record, you can access child records by navigating the relationship. However, this can become complex quickly. It is often cleaner to write separate queries for related tables. This keeps your code readable and easier to debug. Use the getValue() method to read field values and setValue() to change them programmatically.

Debugging Tips

When your script behaves unexpectedly, logging is your best friend. Use the gs.info() or gs.debug() methods to print variable states to the system log. Check the query string you are building to ensure it matches your intent. Verify that the user context has the necessary permissions to access the table. If you are getting silent failures, look at the application logs for error messages that indicate permission denied or invalid field names.

Best Practices for Maintenance

Write clean and modular code. Wrap your GlideRecord logic in functions so they can be reused across different scripts. This makes testing easier and reduces duplication. Always close your GlideRecord objects when you are done, especially in long-running processes. While ServiceNow handles garbage collection, explicit cleanup helps maintain system stability. Use try-catch blocks to handle exceptions gracefully. This prevents your script from crashing the transaction and leaving the database in an inconsistent state.

Remember that GlideRecord scripts run on the server. They cannot access client-side variables or browser information directly. If you need to pass data from the client to the server, use AJAX calls or form actions. Keep your business logic centralized in server-side scripts rather than scattering it across multiple places. This ensures consistency and makes future updates simpler. Test your scripts in a development instance first to verify they work as expected before moving to production environments.

Related

Published 1 December 2026. Found an error? Tell us.