Glide Server APIs
-
- UpdatedAug 1, 2024
- 10 minutes to read
- Xanadu
- Building applications
ServiceNow provides APIs for the Glide Server.
GlideAggregate
The GlideAggregate class is an extension of GlideRecord and allows database aggregation (COUNT, SUM, MIN, MAX, AVG) queries to be done. This can be helpful in creating customized reports or in calculations for calculated fields.
For additional information, refer to GlideAggregate API.
GlideAggregate examples
GlideAggregate is an extension of GlideRecord and its use is probably best shown through a series of examples.
Here is an example that simply gets a count of the number of records in a table:
There is no query associated with the preceding example. If you want to get a count of the incidents that were open, simply add a query as is done with GlideRecord. Here is an example to get a count of the number of active incidents.
To get a count of all the open incidents by category the code is:
The output is:
The following is an example that uses multiple aggregations to see how many times records have been modified using the MIN, MAX, and AVG values.
The output is:
The following is a more complex example that shows how to compare activity from one month to the next.
The output is:
The following is an example to obtain distinct count of a field on a group query.
The output is:
- Three records with a total_cost of $12
- Four records with a total_cost of $10
- Five records with a total_cost of $5
The following code illustrates implementing the SUM aggregate without using the groupBy() method:
The output for this example is:
Using the same data points as the prior example, if you use the groupBy() method, the SUM aggregate returns the sum of all values for the specified field.
The following example illustrates implementing the SUM aggregate using the groupBy() method:
The output for this example is:
GlideRecord
GlideRecord is a special Java class (GlideRecord.java) that can be used in JavaScript exactly as if it was a native JavaScript class.
- is used for database operations instead of writing SQL queries.
- is an object that contains zero or more records from one table. Another way to say this is that a GlideRecord is an ordered list.
A GlideRecord contains both records (rows) and fields (columns). The field names are the same as the underlying database column names. For additional information, refer to GlideRecord - Scoped.
gs.sql())
scripting syntax was discontinued in Geneva. Use standard
GlideRecord syntax in its place.Using GlideRecordSecure
GlideRecordSecure is a class inherited from GlideRecord that performs the same functions as GlideRecord, and also enforces ACLs.
Non-writable fields
Be aware that, when using GlideRecordSecure, non-writable fields are set to NULL when trying to write to the database. By default, canCreate() on the column is replaced with canWrite() on the column. If that returns false, the column value is set to NULL.
Checking for NULL values
Examples
These are two simple examples using GlideRecordSecure.
GlideSystem
The GlideSystem API provides methods for retrieving information.
The GlideSystem (referred to by the variable name 'gs' in business rules) provides a number of convenient methods to get information about the system, the current logged in user, etc. For example, the method addInfoMessage() permits communication with the user.
Many of the GlideSystem methods facilitate the easy inclusion of dates in query ranges and are most often used in filters and reporting.
For additional information, see GlideSystem.
GlideDateTime
The GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.
In addition to the instantiation methods described below, a GlideDateTime object can be
instantiated from a glide_date_time field using the
getGlideObject() method (for example, var gdt =
gr.my_datetime_field.getGlideObject();
).
Some methods use the Java Virtual Machine time zone when retrieving or modifying a date and time value. Using these methods may result in unexpected behavior. Use equivalent local time and UTC methods whenever possible.
GlideDate and GlideDateTime examples
The GlideDate and GlideDateTime APIs are used to manipulate date and time values.
For additional information, refer to GlideDate API and GlideDateTime API.
See also Modify a GlideDateTime field value.
Set a duration field value in script
Examples of JavaScript that can be used to set the value of a duration field.
Using the GlideDateTime.subtract() method
The time values presented to GlideDateTime.subtract are expected to be in the user's time zone and in the user's format.
Setting a default value of a duration field
Setting the default value for a duration field is similar to the method used in the previous topic.
Setting the duration field value in a client script
Calculating and setting a duration using a client script
Here is an example of how to return a value and populate it using a client script.
onChange
client script that includes the following code. You can
modify this script if you need the calculation to happen in an onLoad
script or some other
way.Changing the duration field value
Formatting the Resolve Time
Modify the dictionary entry for the field and add the attribute. If there is an existing attribute, separate multiple attributes with commas.
Setting the maximum unit of measurement
max_unit=minutes
, a duration of 3 hours 5
minutes 15 seconds appears as 185 minutes 15 seconds. To set the maximum unit of duration
measurement, add the following dictionary attribute to the duration
field:Date and time format guidelines
You can specify a date format with a sequence of specific date and time pattern strings. A pattern string consists of one or more uppercase and lowercase letters from A to Z. Any text within quotation marks is ignored and is instead copied into the date output.