Skip to content

Instantly share code, notes, and snippets.

@iamwill
Last active October 24, 2025 05:43
Show Gist options
  • Save iamwill/b545331f2934d6239ba2b85f3c1cf9ec to your computer and use it in GitHub Desktop.
Save iamwill/b545331f2934d6239ba2b85f3c1cf9ec to your computer and use it in GitHub Desktop.
GlideRecord cheatsheet

GlideRecord Cheat Sheet

GlideRecord(String tableName)

Creates an instance of the GlideRecord class for the specified table.

var gr = new GlideRecord('incident');

Methods

Method Description
addActiveQuery() Adds a filter to return active records.
addEncodedQuery(String query) Adds an encoded query to other queries that may have been set.
addNotNullQuery(String fieldName) Adds a filter where fieldName values are not null.
addNullQuery(String fieldName) Adds a filter where fieldName values are null.
addQuery(String fieldName, Object value) Adds a filter where fieldName is equal to value.
addQuery(String fieldName, String operator, Object value) Adds a filter. See Operators
Has Access?
canCreate() Can the user create a record in this table?
canDelete() Can the user delete from this table?
canRead() Can the user read from this table?
canWrite() Can the user write to this table?
Delete  
deleteMultiple() Deletes multiple records that satisfy the query condition.
deleteRecord() Deletes the current record.
Get  
getAttribute(String fieldName) Returns the dictionary attributes for fieldName.
getDisplayValue() Returns the display value for the current record.
getDisplayValue(String fieldName) Returns the display value for fieldName.
getElement(String fieldName) Returns the GlideElement for fieldName.
getEncodedQuery() Returns the current query condition as an encoded query string.
getRecordClassName() Returns the class name for the current record.
getRowCount() Returns the number of rows in the query result.
getTableName() Returns the name of the table associated with the GlideRecord.
getUniqueValue() Returns the primary key of the record, which is usually the sys_id.
getValue(String fieldName) Returns the value for fieldName.
Record Operations  
get(String value) Fetch a record by primary key value, typically sys_id.
get(Object fieldName, Object value) Fetch a record where fieldName equals value.
hasNext() Returns true if there are any more records in the GlideRecord object.
initialize() Creates a GlideRecord without any default values set.
isNewRecord() Returns true if the current record has not yet been inserted into the database.
insert() Inserts a new record.
next() Moves to the next record in the GlideRecord object.
query() Perform the query.
setLimit(int max) Set the maximum number of records to fetch for the query.
setValue(String fieldName, Object value) Sets the value of fieldName.
update(String reason) Save the GlideRecord changes to the database. Reason is saved to the audit record.
updateMultiple() Saves each GlideRecord in the list with any changes that have been made.
Is valid?  
isActionAborted() Checks to see if the current database action is to be aborted.
isValid() Returns true if current table is valid.
isValidField(String fieldName) Returns true if fieldName is valid.
isValidRecord() Returns true if current record is valid.
Order by  
orderBy(String fieldName) Order by fieldName ascending.
orderByDesc(String fieldName) Order by fieldName descending.

addQuery operators

Number fields:

  • =
  • !=
  • >
  • >=
  • <
  • <=

String fields:

  • =
  • !=
  • IN
  • NOT IN
  • STARTSWITH
  • ENDSWITH
  • CONTAINS
  • DOES NOT CONTAIN
  • INSTANCEOF

Examples

addActiveQuery()

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.query();

addEncodedQuery()

var queryString = "priority=1^ORpriority=2";
var gr = new GlideRecord('incident');
gr.addEncodedQuery(queryString);
gr.query();
while (gr.next()) {
  gs.addInfoMessage(gr.number);
}

deleteMultiple()

Do not use deleteMultiple() on tables with currency fields. Always delete each record individually.

var gr = new GlideRecord('incident')
gr.addQuery('active','false'); // delete all inactive incidents
gr.deleteMultiple();

deleteRecord()

var gr = new GlideRecord('incident')
if (gr.get('99ebb4156fa831005be8883e6b3ee4b9'))
    gr.deleteRecord();
}

From:

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordScopedAPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment