OBLYTECH Logo
OBLYTECH Logo

GlideElement and GlideFilter in ServiceNow Server Scripts

A GlideElement is the object a server script gets when it reads a field from a GlideRecord, such as current.priority in a business rule. It exposes the stored value, the display value, permission checks, change tracking and write methods. GlideFilter is a separate object that tests a record already in memory against an encoded query.

Where a GlideElement comes from

Each field on a loaded GlideRecord becomes a GlideElement. Most element methods appear in both the scoped and global APIs, though the lists are not identical. hasValue() and setJournalEntry() are available only in the global API, while getDecryptedValue() and setPhoneNumber() are available only in scoped applications.

The element bridges two representations of one value: the internal database value, such as a choice number or a sys_id, and the display value users see. A script can move between them without writing its own lookup code.

Reading values from a GlideElement

Method What it returns Typical use
getValue() Internal database value Comparisons and building queries
getDisplayValue(maxCharacters) Formatted value, optionally length-capped Messages, notifications and logs
getDisplayValueLang(language) Display value for a BCP-47 language tag Translated messages, with the language plugin
getChoiceValue() Label of the selected choice Readable choice names in logs
getChoices(dependent) Array of valid choices Building option lists
getGlideObject() Platform object for the field type Date arithmetic and timezone work

The display value depends on field type. A choice stores a number and displays its label, so priority 1 displays as “1 – Critical”. A date is stored in UTC and displayed in the user’s time zone. A reference stores a sys_id, while its display value comes from the display field of the referenced record.

Dates show why the distinction matters. In the worked example for opened and SLA due dates, the script converts both fields with getGlideObject(), reads their numeric values and builds a duration. The result reports an eight-hour gap.

Checking permissions and field state

canCreate(), canRead() and canWrite() return Booleans for the current user. Check one before reading or assigning the field.

nil() returns true when the field is null. The global API also offers hasValue(). On a field, hasRightsTo(operationName) checks a named operation for the current user. A separate example uses GlideSecurityManager: it sets another user, fetched by user name, on the security manager and checks whether that user can read an incident record. The output is false for that user, so the same script can answer differently for different people.

Tracking changes to a field

changes() returns true when the field was modified. changesFrom(value) returns true when the previous value matches the object passed in, and changesTo(value) does the same for the new value.

Timing is where people go wrong. Inside an AFTER business rule, changes() still returns true, because previous values are not reset until the database update has finished and the AFTER rules have run. A record that has been read but not written returns false, since before and after values are identical. A common pattern queues an event when an active flag moves from true to false using changesFrom(true). Neither changes() nor changesTo() is supported inside ACL scripts.

Writing values and reporting errors

setValue(value) assigns a value and is the method for new code. setInitialValue() is deprecated in favor of it. setDisplayValue() sets the value users see. setDateNumericValue(milliseconds) sets a duration field from a millisecond count, with no GlideDateTime object to create first.

Errors attach to the field. setError(message) sets a message on a field, and the same method works in scoped applications. The business rule example compares two dates. When the start is after the end, it adds an information message, calls setAbortAction(true) to stop the save and calls setError() on the start field, so the user sees the problem beside the input.

Journal fields have their own helpers. setJournalEntry(entry, userName) adds a work note or comment attributed to a named user, followed by update(). getJournalEntry(1) returns the latest entry, and getJournalEntry(-1) returns all entries separated by blank lines.

For reference fields, getRefRecord() returns a GlideRecord for the referenced row. An empty reference does not return null. It returns an empty GlideRecord, so check isValidRecord() before reading fields from it.

Reading field metadata with GlideElementDescriptor

The descriptor describes the field rather than its value. It has no constructor. Call getED() on a GlideElement or a GlideRecord to obtain one.

getName() returns the element name, such as priority. getLabel() returns the label users see, such as Priority. getLength() returns the size, which is 40 for priority. getInternalType() returns the internal data type, such as integer for priority. isChoiceTable() returns true when the element is defined as a dropdown choice in its dictionary.

Dynamic attribute stores

A dynamic attribute store holds attributes defined in a schema rather than fixed columns. GlideElementDynamicAttributeStore extends the global element API for reading them. Check the access rules on your instance before writing code that creates or reads dynamic attributes.

getDynamicAttributeValue() returns the internal value for an attribute path, and getDynamicAttributeDisplayValue() returns a readable value, falling back to the internal value when no display value exists. The example output also shows the same attribute name twice in stored data, so code that keys a map by attribute name should expect collisions.

Testing a record against a condition with GlideFilter

A GlideRecord query asks the database for matching rows. A GlideFilter asks whether a row you already hold meets the conditions in an encoded query. Construct it with the query and a title, then call match() with a loaded record.

Behavior GlideRecord query GlideFilter
Case handling Case-insensitive Case-sensitive by default
Empty date in a comparison Skipped in the worked example Counted unless ISNOTEMPTY is added
Where it runs Database Record already in memory

The worked example shows the empty-value difference. Among four rows, one with an empty date, a GlideRecord query for dates before today returned three. The same condition through GlideFilter counted four. Adding ISNOTEMPTY brings the count to three.

The match argument controls how conditions combine. On match() it defaults to false, so one satisfied condition is enough. Pass true to require every condition. On the static checkRecord() form it defaults to true. Case sensitivity is a second trap: the static checkRecord() form cannot be switched to case-insensitive, so use match() on a filter instance with setCaseSensitive(false) applied.

Encoded query strings that feed both objects

Encoded query strings are shared by list filters, URL parameters, reference qualifiers and GlideFilter conditions.

Pattern Meaning Example
field=value Equals category=network
field=true Boolean test active=true
field_in_referenced_table=value Follows a reference Assigned_to.roles=itil
choice=stored value The number behind a choice, not its label install_status=1
^ and ^OR AND and OR joins roles=itil^ORroles=admin
fieldLIKEsubstring Contains text rolesLIKEit
fieldINa,b,c Matches any listed value rolesINitil,admin,user

Choice conditions trip people up often. A label such as “Installed” does not match; the stored number does. ORDERBY sorts results, but reference qualifiers do not accept it, and autocomplete order comes from the ref_ac_order_by dictionary attribute instead.

A reliable way to build a string is to let the platform write it. Open a list, construct the filter, click Run, right-click the end of the filter breadcrumb and choose Copy query. Read the copied string before reusing it, because list filter operators may be written differently in the query. For the full operator set, see the encoded query explained page.

Field script patterns that use these objects

Field customization examples show these objects at work. A client onChange script on incident Subcategory looks up a matching row and sets short_description, reading with g_form.getValue() and writing with g_form.setValue(). A server script trims spaces from a user’s first and last name and calls autoSysFields(false) before update().

Client-side logic is covered further on the client script explained page, and the query and update cycle around these element methods is covered on the GlideRecord explained page. Treat these examples with caution. Test them in a non-production instance first.

Common mistakes to avoid

  • Calling changes() or changesTo() inside an ACL script.
  • Expecting changes() to return false in an AFTER business rule after an update.
  • Leaving ISNOTEMPTY out of a GlideFilter condition that involves empty values.
  • Writing a choice condition with the label instead of the stored number.

Frequently asked questions

What is the difference between getValue() and getDisplayValue()?

getValue() returns the internal database value, such as a number for a choice or a sys_id for a reference. getDisplayValue() returns the formatted value users see. Use the internal value for comparisons and queries, and the display value for messages and logs.

Can GlideFilter ignore letter case?

Yes, on a filter instance. GlideFilter is case-sensitive by default, and setCaseSensitive(false) turns case-insensitive matching on for match(). The static checkRecord() form cannot be changed this way.

Why does changes() return true inside an AFTER business rule?

The platform keeps previous values until the database update has finished and the AFTER rules have run. Inside an AFTER rule the previous and current values still differ, so changes() reports true for a field modified in that transaction.

How should a script check whether a user can edit a field?

Use canWrite() on the element for that field.