Most common GlideRecord objects in ServiceNow

ServiceNow's GlideRecord is a powerful API used for database operations. It enables developers to query, insert, update, and delete records within ServiceNow tables efficiently.

Most common GlideRecord Methods

1. query(): Executes the query defined by addQuery() or addEncodedQuery().

This method runs the query set by `addQuery()` or `addEncodedQuery()` and retrieves records from the database.

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

2. newRecord(): Creates a new record in the specified table.

It initializes a new record in memory but does not insert it into the database until `insert()` is called.

var gr = new GlideRecord('incident');
   gr.newRecord();
   gr.short_description = 'New incident created';
   gr.insert();

3. insert(): Inserts the current record into the database.

Used after `newRecord()` to save the record permanently.

   var gr = new GlideRecord('incident');
   gr.newRecord();
   gr.short_description = 'Inserted incident';
   gr.insert();

4. update(): Updates the current record in the database.

Used when modifying existing records.


   var gr = new GlideRecord('incident');
   if (gr.get('number', 'INC0010001')) {
       gr.short_description = 'Updated description';
       gr.update();
   }

5. deleteRecord(): Deletes the current record from the database.

Removes the record permanently if found.


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

6. addQuery(): Adds a query condition to the GlideRecord query.

Used to filter records based on field values.


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

7. addEncodedQuery(): Adds an encoded query to the GlideRecord query.

Encoded queries provide a compact way to filter records with multiple conditions.


   var gr = new GlideRecord('incident');
   gr.addEncodedQuery('priority=1^active=true');
   gr.query();
   

8. next(): Moves to the next record in the GlideRecord result set.

Used in loops to iterate through query results.

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

9. hasNext(): Checks if more records are available in the result set.

Useful for validating if records exist before looping.

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

10. get(): Retrieves a record by sys\_id or key field value.

Fetches a single record that matches the given condition.


    var gr = new GlideRecord('incident');
    if (gr.get('sys_id', 'abc123')) {
        gs.info(gr.short_description);
    }
   

11. orderBy(): Sorts the results in ascending order based on a specified field.

It helps retrieve records in a specific order.


    var gr = new GlideRecord('incident');
    gr.orderBy('priority');
    gr.query();
    

12. orderByDesc(): Sorts the results in descending order based on a specified field.

Used for sorting results in reverse order.


    var gr = new GlideRecord('incident');
    gr.orderByDesc('priority');
    gr.query();
    

13. canCreate(): Checks if the user has permission to create a record.

Ensures security compliance before creating a new record.


    var gr = new GlideRecord('incident');
    if (gr.canCreate()) {
        gs.info('User can create records.');
    }
    

14. canWrite(): Checks if the user has permission to update a record.

Prevents unauthorized modifications.

   
    var gr = new GlideRecord('incident');
    if (gr.canWrite()) {
        gs.info('User can update records.');
    }
    

15. canRead(): Checks if the user has permission to read a record.

Ensures that only authorized users can access the data.

   
    var gr = new GlideRecord('incident');
    if (gr.canRead()) {
        gs.info('User can read records.');
    }

16. canDelete(): Checks if the user has permission to delete a record.

Prevents accidental or unauthorized deletion.

    var gr = new GlideRecord('incident');
    if (gr.canDelete()) {
        gs.info('User can delete records.');
    }

These methods allow ServiceNow developers to efficiently interact with and manipulate data within the platform, ensuring smooth and effective application development. Goodbye until next time.

Up Next
    Ebook Download
    View all
    Learn
    View all