{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":908,"path":"api-reference/server-api-reference/ScriptableDataStreamAPI.md","title":"ScriptableDataStream - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# ScriptableDataStream - Scoped, Global\n\nThe ScriptableDataStream API provides methods to interact with a stream of data.\n\nThis class can only be used in a server-side script after retrieving a ScriptableDataStream object using one of these APIs:\n\n-   The executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n-   The getDataStream\\(\\) method in the ScriptableFlowRunnerResult class. See [ScriptableFlowRunnerResult](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowRunnerResultScopedAPI.md).\n\nAfter retrieving a ScriptableDataStream object, call the methods in this specific order:\n\n1.  Use the [hasNext\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md) method to determine whether there are more items in the data stream.\n2.  Use the [next\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md) method to access the next item in the stream.\n3.  Use the [getItemIndex\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md), [getItemInPageIndex\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md), and [getPageIndex\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md) methods to get information from the stream.\n4.  Use the [close\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableDataStreamAPI.md) method to close the stream.\n\nThis class runs is in the `sn_fd` namespace.\n\n**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.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## ScriptableDataStream - close\\(\\)\n\nCloses the connection to a data stream. Always call this method after performing any desired operations on a data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\n(function() {\n\t\n\ttry {\n​\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_snc_my_scope.data_stream_name');\n​\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n​\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar user = stream.next();\n\t\t\n\t\t\t// Only log the first item in each page\n\t\t\tif (stream.getItemInPageIndex() == 0) {\n\t\t\t\tgs.info('first user on page is ' + user.name);\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n## ScriptableDataStream - getItemIndex\\(\\)\n\nReturns the current index of an item in a data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Current index of an item in a data stream using zero-based indexing.|\n\n```\n(function() {\n\t\n\ttry {\n\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');\n\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar User = stream.next();\n\n\t\t\t// Use the item. Example:\n\t\t\t// var now_GR = new GlideRecord(<table_name>);\n\t\t\t// now_GR.<field_name> = User.<field_name>;\n\t\t\t// now_GR.insert();\n\t\t\n\t\t\t// By default, this code snippet will terminate after 10 items.\n\t\t\t// Remove or modify this limit after your code has been tested.\n\t\t\tif (stream.getItemIndex() >= 9) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n## ScriptableDataStream - getItemInPageIndex\\(\\)\n\nReturns the current index of an item within the current page in a data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Current index of an item within the current page in the data stream using zero-based indexing.|\n\n```\n(function() {\n\t\n\ttry {\n​\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_snc_my_scope.data_stream_name');\n​\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n​\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar user = stream.next();\n\t\t\n\t\t\t// Only log the first item in each page\n\t\t\tif (stream.getItemInPageIndex() == 0) {\n\t\t\t\tgs.info('first user on page is ' + user.name);\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n## ScriptableDataStream - getPageIndex\\(\\)\n\nReturns the current index of a page in a data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|Current index of a page in a data stream using zero-based indexing.|\n\n```\n(function() {\n\t\n\ttry {\n\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');\n\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar item = stream.next();\n\n\t\t\t// Use the item. \n\t\t\tvar now_GR = new GlideRecord('incident');\n\t\t\tnow_GR.setValue('number',item.id);\n\t\t\tnow_GR.setValue('short_description',item.name);\n\t\t\tnow_GR.insert();\n\t\t\n\t\t\t// By default, this code snippet will terminate after 5 pages.\n\t\t\t// Remove or modify this limit after testing your code.\n\t\t\tif (stream.getPageIndex() >= 4) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n## ScriptableDataStream - hasNext\\(\\)\n\nReturns true if there are more items in the data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n**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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_gcx_d43_bjb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag 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.\n-   false: There are no more items in the data stream.\n\n</td></tr></tbody>\n</table>This example creates an incident record for each item returned in the data stream.\n\n```\n(function() {\n\t\n\ttry {\n\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');\n\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar item = stream.next();\n\n\t\t\t// Use the item. \n\t\t\tvar now_GR = new GlideRecord('incident');\n\t\t\tnow_GR.setValue('number',item.id);\n\t\t\tnow_GR.setValue('short_description',item.name);\n\t\t\tnow_GR.insert();\n\t\t\n\t\t\t// By default, this code snippet will terminate after 10 items.\n\t\t\t// Remove or modify this limit after testing your code.\n\t\t\tif (stream.getItemIndex() >= 9) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n## ScriptableDataStream - next\\(\\)\n\nReturns the next item in a data stream.\n\nYou can only call this method on a ScriptableDataStream object returned from the executeDataStreamAction\\(\\) method in the FlowAPI class. See [FlowAPI](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ScriptableFlowAPI.md).\n\n**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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|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.|\n\nThis example creates an incident record for each item returned in the data stream.\n\n```\n(function() {\n\t\n\ttry {\n\n\t\t// Execute Data Stream Action. \n\t\tvar stream = sn_fd.FlowAPI.executeDataStreamAction('x_my_scope.data_stream_name');\n\n\t\t// Process each item in the data stream\n\t\twhile (stream.hasNext()) {\n\n\t\t\t// Get a single item from the data stream.\n\t\t\tvar item = stream.next();\n\n\t\t\t// Use the item. \n\t\t\tvar now_GR = new GlideRecord('incident');\n\t\t\tnow_GR.setValue('number',item.id);\n\t\t\tnow_GR.setValue('short_description',item.name);\n\t\t\tnow_GR.insert();\n\t\t\n\t\t\t// By default, this code snippet will terminate after 10 items.\n\t\t\t// Remove or modify this limit after testing your code.\n\t\t\tif (stream.getItemIndex() >= 9) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\t\t\n\t} catch (ex) {\n\t\tvar message = ex.getMessage();\n\t\tgs.error(message);\n\t} finally {\n\t\tstream.close();\n\t}\n\t\n})();\n```\n\n"}}