Optional - Scoped, Global
-
- UpdatedAug 1, 2024
- 6 minutes to read
- Xanadu
- API reference
The Optional API interacts with a single record returned by the GlideQuery, Stream, or GlideRecord APIs, even when it does not exist. Write scripts that are less likely to result in an error by handling null or undefined query results.
You can get an Optional object in these ways:
- Return an Optional object from these methods in the GlideQuery class. For more information, see GlideQuery.
- Return an Optional object from the find() method in the Stream class. For more information on Stream, see the Stream API.
- Use the lazy() method to generate the value of the Optional if ever needed.
These methods are static and do not require an instance of the class:
You can use these static methods with any API that returns a single value such as GlideRecord.
Use the Optional API in scoped or global server-side scripts. This API requires the GlideQuery [com.sn_glidequery] plugin.
Implementation
This API can work with the GlideQuery and Stream APIs in a builder pattern where the method calls chain together, each method building on the returned result of the previous method. Use methods to define the attributes of the query. The methods do not execute until you call a terminal method, a method that returns a query result, allowing you to define the requirements of the query before executing it.
If the query returns a single record, the system wraps the result in an Optional object. If the query returns a stream of records, the system wraps the result in a Stream object. These objects let you manage the result using a set of methods in each API.
For example, this script performs a query on the Task table, groups the records by priority, and returns each priority that has total reassignments greater than four.
Terminal methods
For performance reasons, a query only fetches data when you call a terminal method. These are the terminal methods from the Optional class:
Optional - empty(String reason)
Returns an empty Optional object. Use this method in an Else clause to handle a query that might not return a result.
Name | Type | Description |
---|---|---|
reason | String | Optional. Reason displayed in the log when Optional.get() is called on the empty Optional object. |
Type | Description |
---|---|
Optional | Object used to interact with a single record. |
Example
This example shows you how to generate an empty Optional object when a query does not return a result.
Output:
Optional - filter(Function predicate)
Applies a predicate function, a function that takes a single value and returns true or false, to the record inside the Optional object. If the function returns true, the method returns the Optional record unchanged. If the function returns false, it returns an empty Optional object.
Name | Type | Description |
---|---|---|
predicate | Function | Predicate function to apply to the value inside the Optional object. Must return a Boolean value. |
Type | Description |
---|---|
Optional | Object used to interact with a single record. |
Example
This example shows you how to apply a filter function to an Optional result.
Optional - flatMap(Function fn)
Applies a function that returns an Optional object to the result of a query. Use this method to perform a second query using the result of the first.
Name | Type | Description |
---|---|---|
fn | Function | Function to apply to the results of the query that returned the Optional object. |
Type | Description |
---|---|
Optional | Object used to interact with a single record. |
Example
This example shows how to execute a query of the User table based on the result of a previous query.
Output:
Optional - get()
Returns the record inside the Optional object, or throws an error if the query does not return a record.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
Any | The record inside the Optional object. If the value is null or undefined, the system throws an error. |
Example
This example shows how to get the value of a single record.
Output:
Optional - ifPresent(Function fn)
Applies a function to the record within an Optional object. If the Optional object does not contain a record, the function does not execute.
Name | Type | Description |
---|---|---|
fn | Function | The function to apply to the record within the Optional object. |
Type | Description |
---|---|
None |
Example
This example shows how to print a value if it exists.
Optional - isEmpty()
Returns true if the Optional object is empty.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
Boolean | Flag that indicates whether the result of a query contains a value. Valid values:
|
Example
This example shows how to check whether the result of a query is empty.
Output:
Optional - isPresent()
Checks whether an Optional object contains a value.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
Boolean | Flag that indicates whether the result of a query contains a value. Valid values:
|
Example
This example shows how to check whether a query returns a result.
Output:
Optional - lazy(Function lazyGetFn)
Returns a new Optional object. Instead of containing the record, the object contains a function to get the record that is only called if and when requested in the code.
Use this method to delay getting the value until it's needed. You might do this if requesting the value from a slow source and don't want to slow down your code unnecessarily. Otherwise, you can return an Optional object using the GlideQuery and Stream APIs.
Name | Type | Description |
---|---|---|
lazyGetFn | Function | Function that returns a single record as a result of a query. For example: |
Type | Description |
---|---|
Optional | Object containing the result of the query in the format
Optional<result> . |
Example
This example shows how to get an Optional object based on a GlideRecord query.
Output:
Optional - map(Function fn)
Applies a function to the result of a query.
Name | Type | Description |
---|---|---|
fn | Function | Function to apply to the result of the query. |
Type | Description |
---|---|
Optional | Object containing the results of the query updated by the function in the
format Optional<result> . |
Example
This example shows how to apply a function that transforms a value to upper case to the result of a query.
Output:
Optional - of(Any value)
Wraps a given value in an Optional object. For example, you can wrap the result of a GlideRecord query in an Optional object to use the associated methods.
Name | Type | Description |
---|---|---|
value | Any | Value inside the Optional object. |
Type | Description |
---|---|
Optional | Object containing the passed-in value in the format
Optional<value> . |
Example
This example shows you how to generate an Optional object based on a GlideRecord query.
Output:
Optional - orElse(Any defaultValue)
Adds a default value within the Optional object if the query does not return any results.
Name | Type | Description |
---|---|---|
defaultValue | Any | Value within the Optional object if the query does not return any results. |
Type | Description |
---|---|
Any | Value within the Optional object if the query does not return any results. |
Example
This example shows how to return a value, even when the query is incorrect.
Output:
On this page
- Implementation
- Terminal methods
- Optional - empty(String reason)
- Optional - filter(Function predicate)
- Optional - flatMap(Function fn)
- Optional - get()
- Optional - ifPresent(Function fn)
- Optional - isEmpty()
- Optional - isPresent()
- Optional - lazy(Function lazyGetFn)
- Optional - map(Function fn)
- Optional - of(Any value)
- Optional - orElse(Any defaultValue)