GlideRecord

Methods

GlideRecord

Methods

addActiveQuery()

Appends active=true to the encoded query of the GlideRecord. Ordering matters, i.e. calling addActiveQuery() before adding a query will append active=true first while calling addActiveQuery() after adding a query will append active=true to the end.

Parameters: None

Returns: String

Return the following string: active=true

Mutates Self?

Yes, the gliderecords encoded query will have active=true appended to it which is a mutation of self.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('nameSTARTSWITHjohn');
userGr.addActiveQuery();

gs.print('Example 1: '+userGr.getEncodedQuery());

userGr.initialize();
userGr.addActiveQuery();
userGr.addEncodedQuery('nameSTARTSWITHjohn');

gs.print('Example 2: '+userGr.getEncodedQuery());

Output

Example 1: nameSTARTSWITHjohn^active=true
Example 2: active=true^nameSTARTSWITHjohn

addDomainQuery

addEncodedQuery(query)

If the GlideRecords internal encoded query is not null, this will append a ^ to the GlideRecord's internal encoded query and then append the query provided in the method's parameters to the GlideRecord's internal encoded query.

If the GlideRecords internal encoded query is null, this will append just the query provided in the method's parameters to the gliderecord's internal encoded query.

Parameters:

  • query: String

Returns: None

Mutates Self?

Yes, the gliderecord's internal encoded query will be modified.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('nameSTARTSWITHjohn');

gs.print('Example 1: '+userGr.getEncodedQuery());

userGr.addEncodedQuery('nameENDSWITHsmith');

gs.print('Example 2: '+userGr.getEncodedQuery());

Output

Example 1: nameSTARTSWITHjohn
Example 2: nameSTARTSWITHjohn^nameENDSWITHsmith

addExtraField(dotWalk)

Allows for better dot walking performance by front loading certain pieces of a child record with the intial encodedQuery.

Parameters:

  • dotWalk: String

Returns: None

Mutates Self?

Yes, the gliderecord's internal encoded query will be modified.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('companyISNOTEMPTY');
userGr.query();
if(userGr.next()) {
	//requires two queries
  gs.print(userGr.company.city.toString());
}


userGr.initialize();
userGr.addEncodedQuery('companyISNOTEMPTY');
userGr.addExtraField('company.city');
userGr.query();
if(userGr.next()) {
  //requires one query
  gs.print(userGr.company.city.toString());
}

addFunction(functionBuilder)

Allows for queries to be built using the GlideDBFunctionBuilder object.

Parameters:

  • functionBuilder: GlideDBFunctionBuilder

Returns: None

Mutates Self?

Yes but, I'm not fully sure what gets mutated though.

Examples:

Script

var fb = new GlideDBFunctionBuilder();
var dbFunction = fb.position();
dbFunction = fb.constant('my'); // search_term: Text to search for in the specified table column.
dbFunction = fb.field('short_description');  // column: Name of the table column to search.
dbFunction = fb.build();

gs.print(dbFunction);

var incidentGr = new GlideRecord('incident'); // Table containing the column to search
incidentGr.addFunction(dbFunction);
incidentGr.addQuery("short_description", "CONTAINS", "my");
incidentGr.setLimit(20);
incidentGr.query();
while(incidentGr.next()) {
  gs.print(incidentGr.short_description + "\n position('my', short_description): " + incidentGr.getValue(dbFunction));
}

Output

glidefunction:position('my',short_description)
My computer is not detecting the headphone device
position('my', short_description): 1
Reset my password
position('my', short_description): 7
Missing my home directory
position('my', short_description): 9
Seem to have an issue with my hard drive...
position('my', short_description): 28
My disk is still having issues. Can't delete a file
position('my', short_description): 1
Reset my password
position('my', short_description): 7
my PDF docs are all locked from editing
position('my', short_description): 1
Printer in my office is out of toner
position('my', short_description): 12
The USB port on my PC stopped working
position('my', short_description): 17
I can't launch my VPN client since the last software update
position('my', short_description): 16
Please remove the latest hotfix from my PC
position('my', short_description): 38
I can't get my weather report
position('my', short_description): 13
My desk phone does not work
position('my', short_description): 1
Can't log into SAP from my laptop today
position('my', short_description): 25
Wireless access is down in my area
position('my', short_description): 28

addInactiveQuery()

Appends active=false to the encoded query of the GlideRecord. Ordering matters, i.e. calling addInctiveQuery() before adding a query will append active=false first while calling addInactiveQuery() after adding a query will append active=false to the end.

Parameters: None

Returns: String

Return the following string: active=false

Mutates Self?

Yes, the gliderecords encoded query will have active=false appended to it which is a mutation of self.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('nameSTARTSWITHjohn');
userGr.addInactiveQuery();

gs.print('Example 1: '+userGr.getEncodedQuery());

userGr.initialize();
userGr.addInactiveQuery();
userGr.addEncodedQuery('nameSTARTSWITHjohn');

gs.print('Example 2: '+userGr.getEncodedQuery());

Output

Example 1: nameSTARTSWITHjohn^active=false
Example 2: active=false^nameSTARTSWITHjohn

addJoinQuery(table, primaryField, foreignField)

Filter query results to records that have matching field values to a foreign table's field.

Parameters:

  • table: String
  • primaryField: String
  • foreignField: String

Returns: String

Returns <primaryField>INnull. I guess this is probably some sort of shorthand for JOIN

Mutates Self?

Yes, the gliderecord's internal encoded query will be modified.

Examples:

Script

var incidentGr = new GlideRecord('incident'); 
incidentGr.addJoinQuery('change_request','caller_id', 'opened_by');
incidentGr.query();
var incident_users = {};
while(incidentGr.next()) {
  incident_users[incidentGr.caller_id.getDisplayValue()] = true;
}

var changeGr = new GlideRecord('change_request'); 
changeGr.addJoinQuery('incident','opened_by','caller_id');
changeGr.query();
var change_users = {};
while(changeGr.next()) {
  change_users[changeGr.opened_by.getDisplayValue()] = true;
}

for(var user in incident_users) {
  gs.print('incident has Change User '+user+'?: '+ change_users[user]);
}

Output

incident has Change User Don Goodliffe?: true
incident has Change User System Administrator?: true

addNotNullQuery(fieldName)

Filters for records where a specific field is not null. This is technically different than ISEMPTY in a query. Basically, null is empty, but empty is not always null. A null field is usually produced by older records where a field may not have existed at the time.

Functionally, this just appends <fieldName>!=NULL to the GlideRecord's internal encoded query.

Parameters:

  • fieldName: String

Returns: String

Returns <fieldName>!=NULL.

Mutates Self?

Yes, the gliderecord's internal encoded query will be modified.

Examples:

Script

//TODO: this is throwing error rn and I don't know why
var incidentGr = new GlideRecord('incident'); 
gs.print(incidentGr.addNotNullQuery('caller_id'));
gs.print(incidentGr.getEncodedQuery());

Output

addNullQuery

Filters for records where a specific field is null. This is technically different than ISNOTEMPTY in a query. Basically, null is empty, but empty is not always null. A null field is usually produced by older records where a field may not have existed at the time.

Functionally, this just appends <fieldName>=NULL to the GlideRecord's internal encoded query.

Parameters:

  • fieldName: String

Returns: String

Returns <fieldName>=NULL.

Mutates Self?

Yes, the gliderecord's internal encoded query will be modified.

Examples:

Script

//TODO: this is throwing error rn and I don't know why
var incidentGr = new GlideRecord('incident'); 
gs.print(incidentGr.addNullQuery('caller_id'));
gs.print(incidentGr.getEncodedQuery());

Output

addQuery(fieldName, op, value)

Helper method to append to encoded query in a more functional way. Instead of building a String and adding with addEncodedQuery, a user can append to the query by providing parameters like addQuery('name', 'STARTSWITH', 'john').

Parameters:

  • fieldName: String
  • op: Option
  • value: String

Returns: String

Returns the encoded query equivalent to what was provided in the methods parameters. Seems to be a bit unrelyable, see example...

Mutates Self?

Yes, the GlideRecord's internal encoded query will be modified.

Operators

Number Operators

  • =: Equals
  • !=: Does not equal
  • >: Greater than
  • >=: Greater than or equal to
  • <: Less than
  • <=: Less than or equal to

String Operators

  • =: Equals
  • !=: Does not equal
  • STARTSWITH: Any String that starts with
  • ENDSWITH: Any String that ends with
  • CONTAINS: Any String that contains
  • LIKE: Any String that contains
  • DOES NOT CONTAIN: Any String that does not contain
  • NOT LIKE: Any String that does not contain
  • IN: Any string that is equal to one of the comma delimited Strings within
  • NOT IN: Any string that is not equal to one of the comma delimited Strings within
  • INSTANCEOF: Any record that is a child class of the class provided in

Examples:

Script

var userGr = new GlideRecord('sys_user');
gs.print(userGr.addQuery('name', 'CONTAINS', 'john'));
gs.print(userGr.getEncodedQuery());

Output

nameLIKEjohn
nameCONTAINSjohn

addQueryHint

To be honest, I can't find any documentation on this or anywhere its used in code. If you know what this does, please submit a pull request or issue please.

addUserQuery(fieldName, op, value)

Helper method to append to encoded query in a more functional way. Instead of building a String and adding with addEncodedQuery, a user can append to the query by providing parameters like addQuery('name', 'STARTSWITH', 'john').

Extends the same functionality as the addQuery() method. However, there are stricter ACL enforments on the query. Good for testing in lower instances.

Parameters:

  • fieldName: String
  • op: Option
  • value: String

Returns: String

Returns the encoded query equivalent to what was provided in the methods parameters. Seems to be a bit unrelyable, see example...

Mutates Self?

Yes, the GlideRecord's internal encoded query will be modified.

Operators

Number Operators

  • =: Equals
  • !=: Does not equal
  • >: Greater than
  • >=: Greater than or equal to
  • <: Less than
  • <=: Less than or equal to

String Operators

  • =: Equals
  • !=: Does not equal
  • STARTSWITH: Any String that starts with
  • ENDSWITH: Any String that ends with
  • CONTAINS: Any String that contains
  • LIKE: Any String that contains
  • DOES NOT CONTAIN: Any String that does not contain
  • NOT LIKE: Any String that does not contain
  • IN: Any string that is equal to one of the comma delimited Strings within
  • NOT IN: Any string that is not equal to one of the comma delimited Strings within
  • INSTANCEOF: Any record that is a child class of the class provided in

Examples:

Script

var userGr = new GlideRecord('sys_user');
gs.print(userGr.addUserQuery('name', 'CONTAINS', 'john'));
gs.print(userGr.getEncodedQuery());

Output

nameLIKEjohn
nameCONTAINSjohn

addValue(fieldName, value)

Atomic adding methods Kind of a strange method. This allows the update counter to be updated at a local level prior to actual database updates with update().

Parameters:

  • fieldName: String
  • value: String

Returns: String

Returns the encoded query equivalent to what was provided in the methods parameters. Seems to be a bit unrelyable, see example...

Mutates Self?

Yes, the GlideRecord's internal encoded query will be modified.

Examples:

Script


Output

appendOrQuery

applyEncodedQuery(query)

Allows fields to be set via encoded query. Useful for populating a form via url, just make sure to properly sanatize things.

Parameters:

  • query: String

Returns: None

Mutates Self?

Yes, the GlideRecord's internal field states are changed based on the encoded query.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.initialize();
userGr.applyEncodedQuery('first_name=john^last_name=smith');
gs.print(userGr.first_name.toString());
gs.print(userGr.last_name.toString());

Output

john
smith

applyRowSecurity

Functionally, this pretty much just adds the functionallity that FilteredGlideRecord and GlideRecordSecure provide by enforcing ACLs during a query.

Parameters: None

Returns: None

Mutates Self?

Yes, not fully sure what, but it has to store some sort of state for this to work.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.applyRowSecurity();
userGr.addEncodedQuery('some query that returns restricted records');
userGr.query();
gs.print(userGr.next()); // no sensitive records are returned, ACLs are enforced

Output

false

applyTemplate(templateName)

Applys a template to a GlideRecord. Use with caution as this function uses a templates name instead of a templates id which means there could be conflicts.

Its much safer to use the GlideTemplate API. See GlideTemplate's apply method for more information.

Parameters:

  • templateName: String

Returns: None

Mutates Self?

Yes, changes the field values of a gliderecord.

Examples:

Script

var userGr = new GlideRecord('sys_user');
userGr.applyTemplate('some template that sets first_name to john');
gs.print(userGr.first_name.toString());

Output

john

attachGlideListener - Undocumented

Parameters:

Returns: None

Mutates Self?

Examples:

Script

Output

autoSysFields(updateFields)

Extends functionality of setWorkflow method to include not updating system fields like sys_updated_on, sys_updated_by, etc.

This method is useful for creating test data for scripts that are dependent on system fields. This method along with setWorkflow(false) achieves basically the same behavior as an xml import of a record.

After using autoSysFields(false) on a GlideRecord in a business rule, make sure to call autoSysFields(true) before the business script ends. I have seen cases where this roles over to following business rules and can have bad consequences.

Parameters:

  • updateFields: bool

Returns: None

Mutates Self?

Yes, this is just a setter for an internal flag.

Examples:

Script

var incidentGr = new GlideRecord('incident');
incidentGr.orderByDesc('sys_created_on');
incidentGr.query();
if (incidentGr.next()) {
  incidentGr.setWorkflow(false);
  incidentGr.autoSysFields(false);
  var current_date_time = (new GlideDateTime()).toString();
  gs.print(incidentGr.sys_updated_on.toString());
  gs.print(current_date_time);
  incidentGr.setValue('sys_created_on', current_date_time);
  incidentGr.setValue('sys_updated_on', current_date_time);
  incidentGr.update();
  incidentGr.setWorkflow(true);
  incidentGr.autoSysFields(true);
  gs.print(incidentGr.sys_updated_on.toString());
}

Output

2025-07-22 15:17:25
2025-08-12 13:50:28
2025-08-12 13:50:28

canCreate()

This method checks against table level ACLs and returns a boolean representing whether the current user can create a record for the current GlideRecord table.

For better performance and grainularity, please see GlideSecurityManager

Parameters: None

Returns: Bool

A boolean representing whether the current user can create a record for the current GlideRecord table.

Mutates Self?

As far as I can tell, no.

Examples:

Script

var incidentGr = new GlideRecord('incident');
gs.print(incidentGr.canCreate());

Output

true

canDelete()

This method checks against table level ACLs and returns a boolean representing whether the current user can delete a record for the current GlideRecord table.

For better performance and grainularity, please see GlideSecurityManager

Parameters: None

Returns: Bool

A boolean representing whether the current user can delete a record for the current GlideRecord table.

Mutates Self?

As far as I can tell, no.

Examples:

Script

var incidentGr = new GlideRecord('incident');
gs.print(incidentGr.canDelete());

Output

true

canRead()

This method checks against table level ACLs and returns a boolean representing whether the current user can read a record for the current GlideRecord table.

For better performance and grainularity, please see GlideSecurityManager

Parameters: None

Returns: Bool

A boolean representing whether the current user can read a record for the current GlideRecord table.

Mutates Self?

As far as I can tell, no.

Examples:

Script

var incidentGr = new GlideRecord('incident');
gs.print(incidentGr.canRead());

Output

true

canWrite()

This method checks against table level ACLs and returns a boolean representing whether the current user can write a record for the current GlideRecord table.

For better performance and grainularity, please see GlideSecurityManager

Parameters: None

Returns: Bool

A boolean representing whether the current user can write a record for the current GlideRecord table.

Mutates Self?

As far as I can tell, no.

Examples:

Script

var incidentGr = new GlideRecord('incident');
gs.print(incidentGr.canWrite());

Output

true

changes()

This method is used to tell if a field within a GlideRecord changes in a business rule.

Parameters: None

Returns: Bool

A boolean representing whether the current field for a record has changed in a business rule.

Mutates Self?

As far as I can tell, no.

Examples:

Script

//current is a GlideRecord('incident') in business rule's script
gs.print(current.sys_updated_on.changes());

Output

true

chooseWindow(firstRow, lastRow, forceCount)

This method is useful to add a starting offset to GlideRecord's query. This can usually be done just by adding to an encoded query, but this is usually a bit more readable.

Parameters:

  • firstRow: integer
  • lastRow: integer
  • forceCount: integer

Returns: None

Mutates Self?

Yes

Examples:

Script

var incidentGr = new GlideRecord('incident');
incidentGr.chooseWindow(5,10,5);
incidentGr.orderBy('number');
incidentGr.query();
gs.print(incidentGr.getRowCount());
while(incidentGr.next()){
  gs.print(incidentGr.number.toString());
}

Output

67
INC0000006
INC0000007
INC0000008
INC0000009
INC0000010

close - undocumented

createElement - undocumented

createIndex - undocumented

deleteMultiple()

Delete all records that meet the criteria provided by the GlideRecord's query.

This is done at the Database level, instead of from the javascript runtime, so the performance is much better.

Parameters: None

Returns: None

Mutates Self?

Nope

Examples:

Script

var syslogGr = new GlideRecord('syslog');
syslogGr.addEncodedQuery('message=a lot of spam');
syslogGr.query();
while(syslogGr.next()) {
  //bad performance
  syslogGr.deleteRecord();
}

var syslogGr = new GlideRecord('syslog');
syslogGr.addEncodedQuery('message=a lot of spam');
syslogGr.query();
//good performance
syslogGr.deleteMultiple();

deleteRecord()

Deletes the current GlideRecord.

Parameters: None

Returns: None

Mutates Self?

Yes

Examples:

Script

var incidentGr = new GlideRecord('incident');
incidentGr.query();
if(incidentGr.next()) {
  var sys_id = incidentGr.sys_id.toString();
  incidentGr.deleteRecord();
  if(incidentGr.get(sys_id)) {
    gs.print('record found');
  } else {
    gs.print('record not found');
  }
}

Output

record not found

disableSecurityFeature

disableSysIdInOptimization

Disables certain indexing optomizations when querying by a large set of IDs.

Parameters: None

Returns: None

Mutates Self?

Yes

Examples:

Script

var a_large_list_of_sys_ids = [
// 200 ids...
].toString();
var incidentGr = new GlideRecord('incident');
incidentGr.addEncodedQuery('sys_idIN'+a_large_list_of_sys_ids);
incidentGr.query();
gs.print(incidentGr.getRowCount());
incidentGr.addEncodedQuery('sys_idIN'+a_large_list_of_sys_ids);
incidentGr.disableSysIdInOptimization();
incidentGr.query();
gs.print(incidentGr.getRowCount());

Output

possibly an incorrect count
200

dropIndex

enableSecurityFeature

enableSessionLanguageJoin

evaluateAsDefault

find

findForeignKey

get

getAttribute

getBooleanAttribute

getCategory

getClassDisplayValue

getDisplayName

getDisplayValue

getDynamicAttribute

getDynamicAttributeDisplayValue

getDynamicAttributeValue

getED

getElement

getElements

getEncodedQuery

getEngineParameter

getEscapedDisplayValue

getFields

getGeoPoint

getLabel

getLastErrorMessage

getLink

getLocation

getPlural

getRecordClassName

getRelatedLists

getRelatedRecords

getRelatedTables

getRequestedElements

getRowCount

getRowNumber

getSetRowCount

getTableName

getTableScope

getTableScopeId

getTableScopeName

getUniqueValue

getValue

getViewDefinition

hasAttachments

hasClass

hasNext

hasOwnProperty

hasRightsTo

incrementViewCount

initialize

insert

insertLazy

insertOrUpdate

insertWithReferences

instanceOf

isActionAborted

isDataFabricOperation

isEncodedQueryValid

isForeignTable

isInGlobalScope

isInSelectedScope

isInStoreScope

isMetadata

isNewRecord

isPrototypeOf

isReadonly

isValid

isValidEncodedQuery

isValidField

isValidMetadataRecord

isValidRecord

isView

isWorkflow

largeResultExpected

makeReadonly

moreEncodedQuery

newGlideRecordNamed

newRecord

next

nextRecord

notifyUser

onePassQuery

operation

orderBy

orderByDesc

popCurrent

propertyIsEnumerable

putCurrent

putOptimizers

query

queryNoDomain

restoreLocation

saveLocation

scheduleScript

setAbortAction

setCategory

setDisplayValue

setDynamicAttributeDisplayValue

setDynamicAttributeDisplayValues

setDynamicAttributeValue

setDynamicAttributeValues

setEngineParameter

setForceUpdate

setLimit

setLocation

setNewGuid

setNewGuidValue

setNoCount

setQueryReferences

setSystem

setUseEngines

setValue

setViewDefinition

setWorkflow

targetExtension

toLocaleString

toSource

toString

update

updateElement

updateLazy

updateMultiple

updateMultipleAllowNull

updateNoDomain

updateWithReferences

GlideDBFunctionBuilder

Provides methods to build SQL operation functions, e.g. coalesce, concat, compare, etc.

Methods

GlideDBFunctionBuilder

Provides methods to build SQL operation functions, e.g. coalesce, concat, compare, etc.

Methods

GlideTemplate (wip)

GlideTemplate (wip)

apply(templateId)

create (wip)

equals (wip)

getApplicable (wip)

getClass (wip)

getTemplateElements (wip)

hashCode (wip)

isSimple (wip)

notify (wip)

notifyAll (wip)

setApplyChildren (wip)

setDoInsert (wip)

setFields (wip)

toString (wip)

wait (wip)

GlideSecurityManager (wip)

Table API

Encoded Query Easter Eggs

Snippets

A variety of code snippets that are useful/common in ServiceNow but are a pain to often find.