Most Used GlideRecord Methods in ServiceNow

GlideRecord is at the heart of ServiceNow's scripting capabilities, which offer a strong framework for workflow management and automation. This API is a crucial tool for carrying out CRUD (Create, Read, Update, Delete) operations within ServiceNow since it enables developers to work with database tables effectively. You can greatly improve your ability to work with records dynamically by being familiar with the most popular GlideRecord techniques.

GlideRecord

A server-side API called GlideRecord is used to query and modify records in ServiceNow tables. It offers a programmatic approach to record retrieval, updating, insertion, and deletion while preserving system security and integrity. GlideRecord streamlines database interactions, whether you're working with issues, change requests, or custom tables.

1. initialize()

Used to initialize a new record without inserting it into the database. This is particularly useful when you want to set values before saving the record.

Example

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'New incident record';
gr.insert();

2. newRecord()

Check if a GlideRecord instance represents a new record that has not been inserted yet. This method is helpful in scenarios where you need to confirm that the record hasn't been previously created.

Example

var gr = new GlideRecord('incident');
gr.initialize();
if (gr.newRecord()) {
    gs.info('This is a new record');
}

3. addQuery()

Used to filter records based on specific conditions. This method is commonly used in scripts that require fetching records dynamically.

Example

var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

4. addEncodedQuery()

Allows filtering using an encoded query string. This is useful when dealing with complex query conditions.

Example

var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^state=2');
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

5. get()

Fetches a specific record using its Sys ID or a unique field. This method is handy when you need to retrieve a single record efficiently.

Example

var gr = new GlideRecord('incident');
if (gr.get('sys_id', '46d44f9c1b232010d6a45f6f1d4bcb8e')) {
    gs.info(gr.number);
}

6. next()

Moves to the next record in the result set, often used in loops to iterate over multiple records.

Example

var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

7. hasNext()

Check if there is another record available after the current one. This method is particularly useful when processing records in batches.

Example

var gr = new GlideRecord('incident');
gr.query();
if (gr.hasNext()) {
    gs.info('There are more records to process.');
}

8. setLimit()

Limits the number of records returned, improving performance by avoiding excessive queries.

Example

var gr = new GlideRecord('incident');
gr.setLimit(5);
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

9. insert()

Inserts a new record into the database. Useful when automating the creation of records in workflows.

Example

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'Created via script';
gr.insert();

10. update()

Updates an existing record in the database. A common use case is modifying an incident’s status or priority.

Example

var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
    gr.state = 2;
    gr.update();
}

11. deleteRecord()

Deletes the current record. Be cautious when using this method, as it permanently removes data.

Example

var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
    gr.deleteRecord();
}

12. orderBy() & orderByDesc()

Sorts records in ascending or descending order, making it easier to retrieve ordered datasets.

Example

var gr = new GlideRecord('incident');
gr.orderBy('priority');
gr.query();
while (gr.next()) {
    gs.info(gr.number);
}

13. getRowCount()

Returns the number of records retrieved, useful for generating reports or performance optimization.

Example

var gr = new GlideRecord('incident');
gr.query();
gs.info('Total incidents: ' + gr.getRowCount());

14. setValue()

Sets the value of a field programmatically.

Example

var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
    gr.setValue('state', 3);
    gr.update();
}

Mastering these GlideRecord methods will enable you to interact effectively with ServiceNow’s database, streamline automation, and optimize system performance. Whether you are developing custom applications, troubleshooting issues, or writing business rules, understanding how to leverage GlideRecord efficiently will make your scripting more powerful and effective. Keep exploring, keep learning, and happy scripting! Goodbye until the next article.

Up Next
    Ebook Download
    View all
    Learn
    View all