Stream - Scoped, Global
-
- UpdatedAug 1, 2024
- 9 minutes to read
- Xanadu
- API reference
The Stream API provides methods to interact with a stream of items such as records. For example, you can use the forEach() method to update the state of each record in a stream returned by the GlideQuery API.
You can get a Stream object in these ways:
- Instantiate a Stream object using the constructor.
- Return a Stream object from the GlideQuery.select() method. For more information, see GlideQuery.
This method is static and does not require an instance of the class: fromArray().
Use the Stream 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 Optional 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 Stream class:
Stream - Stream(Function nextFn)
Instantiates a Stream object.
Instead of using this constructor, you can return a Stream object based on a query using the GlideQuery API.
Name | Type | Description |
---|---|---|
nextFn | Function | A function that retrieves the next item in the stream. |
Example
This example shows you how to create a Stream object based on a random number generator. Make sure to include the limit() method to avoid creating an infinite loop.
Output:
Stream - chunk(Number count)
Returns results in batches of arrays, each containing the number of records passed to the method.
Name | Type | Description |
---|---|---|
count | Number | Number of records in each array returned from the stream. |
Type | Description |
---|---|
Stream | Object used to interact with a stream of items such as records. |
Example
This example shows how to query a table and chunk the result into batches of arrays.
Output:
Example
This example shows how to create a child query using batches of IDs. When you call the flatMap() method after using the chunk() method, the system iterates over the batch of records instead of each individual record.
Stream - every(Function predicate)
Applies a predicate function to every item in the Stream object. If the predicate returns true for every item in the stream, the method returns true. If the predicate returns false for any item in the stream, the method returns false.
Name | Type | Description |
---|---|---|
predicate | Function | Predicate function to apply to every record or item inside the Stream object. The function must take each item in the stream as input and return a boolean. |
Type | Description |
---|---|
Boolean | Flag that indicates whether the predicate function returns true for every item in the stream. Valid values:
|
Example
This example shows how to apply a predicate function to every item in a stream.
Output:
Stream - filter(Function predicate)
Applies a predicate function to each item in the Stream object. If the predicate returns true, the method returns the stream. If the predicate returns false, it returns an empty Stream object.
For better performance, use the where(), whereNotNull(), and whereNull() methods in the GlideQuery class instead of this method where possible. See GlideQuery.
Name | Type | Description |
---|---|---|
predicate | Function | Predicate function to apply to every record or item inside the Stream object. The function must take each item in the stream as input and return a boolean. |
Type | Description |
---|---|
Stream | Object used to interact with a stream of items such as records. |
Example
This example shows how to check all records in the Task table against a defined filter. If the records match the filter, the system returns the stream of records. Otherwise, it returns an empty Stream object.
Stream - find(Function predicate)
Returns the first record or item in the Stream object that matches the predicate function. If no predicate function is provided, then the method returns the first record or item in the Stream.
Name | Type | Description |
---|---|---|
predicate | Function | Optional. Predicate function to apply to the items inside the Stream object. The function must take each item in the stream as input and return a boolean. |
Type | Description |
---|---|
Optional | Object containing the returned record. |
Example
This example shows how to return the first record from the stream.
Output:
Stream - flatMap(function fn)
Applies a function to every item in a stream. Returns another stream that you can iterate over.
Use this method instead of map() if the function returns a second stream of records.
Name | Type | Description |
---|---|---|
fn | Function | Function to apply to the result of the query that returns a Stream object. |
Type | Description |
---|---|
Stream | Object containing the stream of records updated after applying the function. |
Example
This example shows you how to query the User table and then create a child query from the result. This example executes an N+1 query, which can cause performance issues. Avoid this use case in a production environment.
Output:
Stream - forEach(Function fn)
Applies the specified function to each record or item in the stream.
Name | Type | Description |
---|---|---|
fn | Function | Function to apply to each item in the stream. |
Type | Description |
---|---|
None |
Example
This example shows you how to print the result of each item in the stream.
Output:
Stream - fromArray(Object arr)
Returns a Stream object that contains the values from the provided array.
Name | Type | Description |
---|---|---|
arr | Array | Array of values to create the stream from. |
Type | Description |
---|---|
Stream | Object used to interact with a stream of items such as records. |
Example
This example shows how to create a Stream object containing an array of values.
Output:
Stream - limit(Number count)
Limits the number of results returned by the stream.
For better performance, use the limit() method in the GlideQuery class where possible. See GlideQuery. You may need to use this method to limit results with the Stream.flatMap() method.
Name | Type | Description |
---|---|---|
count | Number | Number of records to return. |
Type | Description |
---|---|
Stream | Object used to interact with a stream of items such as records. |
Example
This example shows you how to limit the results returned from the Stream.flatMap() method.
Output:
Stream - map(Function fn)
Applies a function to each item in a stream and returns the updated Stream object.
Name | Type | Description |
---|---|---|
fn | Function | Function to apply to the result of the query that takes the each item in the stream as input. |
Type | Description |
---|---|
Stream | Object containing the stream of records updated after applying the function. |
Example
This example shows you how to apply a function to every item in the stream.
Output:
Stream - reduce(function reducerFn, Any initialValue)
Executes a reducer function on each item in the stream, resulting in single output value.
This method is similar to the native JavaScript reduce() method. For more information, see w3schools documentation.
Name | Type | Description |
---|---|---|
reducerFn | Function | Function to apply to each item in the stream that reduces the stream to a
single value. This function must take two arguments:
|
initialValue | Any | Value passed to the function as the initial value. |
Type | Description |
---|---|
Any | Accumulated total of all items returned by the reducer function. |
Example
This example shows you how to return the record with the longest name from the User table.
Output:
Stream - some(Function predicate)
Applies a predicate function, a function that takes a single value and returns true or false, to each item in the stream. If the predicate returns true for any item in the stream, the method returns true.
Name | Type | Description |
---|---|---|
predicate | Function | Predicate function to apply to the items inside the Stream object. Must return a Boolean value. |
Type | Description |
---|---|
Boolean | Flag that indicates whether the predicate function returned true for an item in the stream. Valid values:
|
Example
This example shows how to check whether any descriptions in the Task table are over 1,000 characters long.
Output:
Stream - toArray(Number count)
Returns an array containing the given number of items from the stream.
Name | Type | Description |
---|---|---|
count | Number | The maximum number of items from the stream to return in the array. |
Type | Description |
---|---|
Array | Array containing the given number of items from the stream. |
Example
This example shows you how to transform a stream of records into a JavaScript array.
Output:
On this page
- Implementation
- Terminal methods
- Stream - Stream(Function nextFn)
- Stream - chunk(Number count)
- Stream - every(Function predicate)
- Stream - filter(Function predicate)
- Stream - find(Function predicate)
- Stream - flatMap(function fn)
- Stream - forEach(Function fn)
- Stream - fromArray(Object arr)
- Stream - limit(Number count)
- Stream - map(Function fn)
- Stream - reduce(function reducerFn, Any initialValue)
- Stream - some(Function predicate)
- Stream - toArray(Number count)