---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# ScriptableDataStream - Scoped, Global

# ScriptableDataStream - Scoped, Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 5 minutes to read

The ScriptableDataStream API provides methods to interact with a
stream of data.

This class can only be used in a server-side script after retrieving a ScriptableDataStream
object using one of these APIs:  
* The executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").
* The getDataStream() method in the ScriptableFlowRunnerResult class. See [ScriptableFlowRunnerResult](https://servicenow-prod.fluidtopics.net/2DHowZp00mH_HJe0GOhdUw#ScriptableFlowRunnerResultScopedAPI "Captures the result of using ScriptableFlowRunner to execute a flow, subflow, or action. Includes data such as the context ID, domain, and any outputs from the flow execution.").
{#ScriptableDataStreamAPI__ul_wnh_sfv_ylb}

After retrieving a ScriptableDataStream object, call the methods in this specific order:  
1. Use the [hasNext()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-hasNext "Returns true if there are more items in the data stream.") method to determine whether there are more items in the data stream.
2. Use the [next()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-next "Returns the next item in a data stream.") method to access the next item in the stream.
3. Use the [getItemIndex()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-getItemIndex "Returns the current index of an item in a data stream."), [getItemInPageIndex()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-getItemInPageIndex "Returns the current index of an item within the current page in a data stream."), and [getPageIndex()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-getPageIndex "Returns the current index of a page in a data stream.") methods to get information from the stream.
4. Use the [close()](https://servicenow-prod.fluidtopics.net/~CG6KzOxPxol~TpeNSp2HA#ScriptableDS-close "Closes the connection to a data stream. Always call this method after performing any desired operations on a data stream.") method to close the stream.
{#ScriptableDataStreamAPI__ol_k1k_yp1_3jb}

This class runs is in the `sn_fd` namespace.  
Note:  
Always wrap data stream logic in a `try/catch` block to catch errors. Always include a `finally` statement that ends with the close() method from the ScriptableDataStream class to close the data stream and prevent performance issues.

## ScriptableDataStream - close() {#ariaid-title2}

Closes the connection to a data stream. Always call this method after performing any
desired operations on a data stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").
{#ScriptableDS-close__table_idv_glc_cjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#ScriptableDS-close__table_idv_glc_cjb} {#ScriptableDS-close__table_jdv_glc_cjb__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 2. Returns]

{#ScriptableDS-close__table_jdv_glc_cjb}  

    (function() {
    	
    	try {
    ​
    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_snc_my_scope.data_stream_name');
    ​
    		// Process each item in the data stream
    		while (stream.hasNext()) {
    ​
    			// Get a single item from the data stream.
    			var user = stream.next();
    		
    			// Only log the first item in each page
    			if (stream.getItemInPageIndex() == 0) {
    				gs.info('first user on page is ' + user.name);
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();

## ScriptableDataStream - getItemIndex() {#ariaid-title3}

Returns the current index of an item in a data stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").
{#ScriptableDS-getItemIndex__table_ic4_flc_cjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 3. Parameters]

{#ScriptableDS-getItemIndex__table_ic4_flc_cjb} {#ScriptableDS-getItemIndex__table_jc4_flc_cjb__entry__2}

| Type | Description |
|-|-|
| Number | Current index of an item in a data stream using zero-based indexing. |
[Table 4. Returns]

{#ScriptableDS-getItemIndex__table_jc4_flc_cjb}  

    (function() {
    	
    	try {

    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');

    		// Process each item in the data stream
    		while (stream.hasNext()) {

    			// Get a single item from the data stream.
    			var User = stream.next();

    			// Use the item. Example:
    			// var now_GR = new GlideRecord(<table_name>);
    			// now_GR.<field_name> = User.<field_name>;
    			// now_GR.insert();
    		
    			// By default, this code snippet will terminate after 10 items.
    			// Remove or modify this limit after your code has been tested.
    			if (stream.getItemIndex() >= 9) {
    				break;
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();

## ScriptableDataStream - getItemInPageIndex() {#ariaid-title4}

Returns the current index of an item within the current page in a data
stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").
{#ScriptableDS-getItemInPageIndex__table_mg4_zkc_cjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 5. Parameters]

{#ScriptableDS-getItemInPageIndex__table_mg4_zkc_cjb} {#ScriptableDS-getItemInPageIndex__table_ng4_zkc_cjb__entry__2}

| Type | Description |
|-|-|
| Number | Current index of an item within the current page in the data stream using zero-based indexing. |
[Table 6. Returns]

{#ScriptableDS-getItemInPageIndex__table_ng4_zkc_cjb}  

    (function() {
    	
    	try {
    ​
    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_snc_my_scope.data_stream_name');
    ​
    		// Process each item in the data stream
    		while (stream.hasNext()) {
    ​
    			// Get a single item from the data stream.
    			var user = stream.next();
    		
    			// Only log the first item in each page
    			if (stream.getItemInPageIndex() == 0) {
    				gs.info('first user on page is ' + user.name);
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();

## ScriptableDataStream - getPageIndex() {#ariaid-title5}

Returns the current index of a page in a data stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").
{#ScriptableDS-getPageIndex__table_l3t_g43_bjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 7. Parameters]

{#ScriptableDS-getPageIndex__table_l3t_g43_bjb} {#ScriptableDS-getPageIndex__table_m3t_g43_bjb__entry__2}

| Type | Description |
|-|-|
| Number | Current index of a page in a data stream using zero-based indexing. |
[Table 8. Returns]

{#ScriptableDS-getPageIndex__table_m3t_g43_bjb}  

    (function() {
    	
    	try {

    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');

    		// Process each item in the data stream
    		while (stream.hasNext()) {

    			// Get a single item from the data stream.
    			var item = stream.next();

    			// Use the item. 
    			var now_GR = new GlideRecord('incident');
    			now_GR.setValue('number',item.id);
    			now_GR.setValue('short_description',item.name);
    			now_GR.insert();
    		
    			// By default, this code snippet will terminate after 5 pages.
    			// Remove or modify this limit after testing your code.
    			if (stream.getPageIndex() >= 4) {
    				break;
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();

## ScriptableDataStream - hasNext() {#ariaid-title6}

Returns true if there are more items in the data stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").  
Note:  
By default, the instance waits for 600 seconds to retrieve a single page of data from a MID Server. If you encounter a timeout when running a Data Stream action through a MID Server, change this default by increasing the datastream_alternative_env_fetch_page_timeout_seconds system property.
{#ScriptableDS-hasNext__table_fcx_d43_bjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 9. Parameters]

{#ScriptableDS-hasNext__table_fcx_d43_bjb} {#ScriptableDS-hasNext__table_gcx_d43_bjb__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that determines whether there are more items in the data stream. Values include: * true: There are more items to iterate through in the data stream. * false: There are no more items in the data stream. {#ScriptableDS-hasNext__ul_xy5_z4d_cjb} |
[Table 10. Returns]

{#ScriptableDS-hasNext__table_gcx_d43_bjb}  
This example creates an incident record for each item returned in the data stream.

    (function() {
    	
    	try {

    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');

    		// Process each item in the data stream
    		while (stream.hasNext()) {

    			// Get a single item from the data stream.
    			var item = stream.next();

    			// Use the item. 
    			var now_GR = new GlideRecord('incident');
    			now_GR.setValue('number',item.id);
    			now_GR.setValue('short_description',item.name);
    			now_GR.insert();
    		
    			// By default, this code snippet will terminate after 10 items.
    			// Remove or modify this limit after testing your code.
    			if (stream.getItemIndex() >= 9) {
    				break;
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();

## ScriptableDataStream - next() {#ariaid-title7}

Returns the next item in a data stream.
You can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction() method in the FlowAPI class. See [FlowAPI](https://servicenow-prod.fluidtopics.net/oW~16xYu7YXh0S2TGiCBRw#ScriptableFlowAPI "The FlowAPI provides methods to execute actions, flows, or subflows in server-side scripts using either blocking or non-blocking methods.").  
Note:  
By default, the instance waits for 600 seconds to retrieve a single page of data from a MID Server. If you encounter a timeout when running a Data Stream action through a MID Server, change this default by increasing the datastream_alternative_env_fetch_page_timeout_seconds system property.
{#ScriptableDS-next__table_lrc_f43_bjb__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 11. Parameters]

{#ScriptableDS-next__table_lrc_f43_bjb} {#ScriptableDS-next__table_mrc_f43_bjb__entry__2}

| Type | Description |
|-|-|
| Object | The next item in the data stream. This object contains the outputs defined by the Data Stream action. To view the Data Stream action outputs, navigate to the Outputs section of the Data Stream action in the Flow Designer interface. |
[Table 12. Returns]

{#ScriptableDS-next__table_mrc_f43_bjb}  
This example creates an incident record for each item returned in the data stream.

    (function() {
    	
    	try {

    		// Execute Data Stream Action. 
    		var stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');

    		// Process each item in the data stream
    		while (stream.hasNext()) {

    			// Get a single item from the data stream.
    			var item = stream.next();

    			// Use the item. 
    			var now_GR = new GlideRecord('incident');
    			now_GR.setValue('number',item.id);
    			now_GR.setValue('short_description',item.name);
    			now_GR.insert();
    		
    			// By default, this code snippet will terminate after 10 items.
    			// Remove or modify this limit after testing your code.
    			if (stream.getItemIndex() >= 9) {
    				break;
    			}
    		}		
    	} catch (ex) {
    		var message = ex.getMessage();
    		gs.error(message);
    	} finally {
    		stream.close();
    	}
    	
    })();


