---
sourceDocument: Xanadu API リファレンス
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu API リファレンス

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# ScriptableDataStream - スコープ指定、グローバル

# ScriptableDataStream - スコープ指定、グローバル {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：12分

ScriptableDataStream API は、データのストリームとやり取りするためのメソッドを提供します。

このクラスは、次のいずれかの API を使用して ScriptableDataStream オブジェクトを取得した後のサーバー側スクリプトでのみ使用できます。  
* FlowAPI クラスの executeDataStreamAction() メソッド。「 [FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。
* ScriptableFlowRunnerResult クラスの getDataStream() メソッド。「[ScriptableFlowRunnerResult](https://servicenow-prod.fluidtopics.net/c8I2bjR6f125De7BnO4UoA#ScriptableFlowRunnerResultScopedAPI "ScriptableFlowRunner を使用してフロー、サブフロー、またはアクションを実行した結果をキャプチャします。コンテキスト ID、ドメイン、フロー実行の出力などのデータが含まれます。")」を参照して下さい。
{#ScriptableDataStreamAPI__ul_wnh_sfv_ylb}

ScriptableDataStream オブジェクトを取得した後、次の順序でメソッドを呼び出します。  
1. [hasNext()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-hasNext "データストリームにさらにアイテムがある場合は true を返します。") メソッドを使用して、データストリーム内にまだアイテムがあるかどうかを判断します。
2. [next()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-next "データストリーム内の次のアイテムを返します。") メソッドを使用して、ストリーム内の次のアイテムにアクセスします。
3. [getItemIndex()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-getItemIndex "データストリーム内のアイテムの現在のインデックスを返します。")、[getItemInPageIndex()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-getItemInPageIndex "データストリームの現在のページにあるアイテムの現在のインデックスを返します。")、および [getPageIndex()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-getPageIndex "データストリーム内のページの現在のインデックスを返します。") メソッドを使用して、ストリームから情報を取得します。
4. [close()](https://servicenow-prod.fluidtopics.net/Am6spO0jzjjQ~pRu8enokQ#ScriptableDS-close "データストリームへの接続を閉じます。データストリームで必要な操作を実行した後は、常にこのメソッドを呼び出します。") メソッドを使用してストリームを閉じます。
{#ScriptableDataStreamAPI__ol_k1k_yp1_3jb}

このクラスは `sn_fd` 名前空間で実行されます。  
注:  
エラーを捕捉するために、常にデータストリームのロジックを `try/catch` ブロックで囲みます。データストリームを閉じてパフォーマンスの問題を防ぐために、常に、ScriptableDataStream クラスの close() メソッドで終了する `finally` ステートメントを含めます。

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

データストリームへの接続を閉じます。データストリームで必要な操作を実行した後は、常にこのメソッドを呼び出します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。
{#ScriptableDS-close__table_idv_glc_cjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 1. パラメーター]

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

| タイプ | 説明 |
|-|-|
| なし |   |
[表 : 2. 返される内容]

{#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}

データストリーム内のアイテムの現在のインデックスを返します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。
{#ScriptableDS-getItemIndex__table_ic4_flc_cjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 3. パラメーター]

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

| タイプ | 説明 |
|-|-|
| 数字 | ゼロベースのインデックスを使用したデータストリーム内のアイテムの現在のインデックス。 |
[表 : 4. 返される内容]

{#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}

データストリームの現在のページにあるアイテムの現在のインデックスを返します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。
{#ScriptableDS-getItemInPageIndex__table_mg4_zkc_cjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 5. パラメーター]

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

| タイプ | 説明 |
|-|-|
| 数字 | ゼロベースのインデックスを使用したデータストリームの現在のページにあるアイテムの現在のインデックス。 |
[表 : 6. 返される内容]

{#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}

データストリーム内のページの現在のインデックスを返します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。
{#ScriptableDS-getPageIndex__table_l3t_g43_bjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 7. パラメーター]

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

| タイプ | 説明 |
|-|-|
| 数字 | ゼロベースのインデックスを使用したデータストリーム内のページの現在のインデックス。 |
[表 : 8. 返される内容]

{#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}

データストリームにさらにアイテムがある場合は true を返します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。  
注:  
デフォルトでは、1 ページのデータを MID サーバー から取得する際にインスタンスは 600 秒間待機します。MID サーバー で データストリーム アクションを実行しているときにタイムアウトが発生した場合は、datastream_alternative_env_fetch_page_timeout_seconds システムプロパティを増やしてこのデフォルトを変更します。
{#ScriptableDS-hasNext__table_fcx_d43_bjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 9. パラメーター]

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

| タイプ | 説明 |
|-|-|
| ブーリアン | データストリーム内にまだアイテムがあるかどうかを判断するフラグ。次の値が含まれます。 * true：データストリーム内で反復するアイテムがまだある。 * false：データストリームにこれ以上アイテムはない。 {#ScriptableDS-hasNext__ul_xy5_z4d_cjb} |
[表 : 10. 返される内容]

{#ScriptableDS-hasNext__table_gcx_d43_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 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}

データストリーム内の次のアイテムを返します。
このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「[FlowAPI](https://servicenow-prod.fluidtopics.net/3iPJndAVdw6y5bMCP2lRYQ#ScriptableFlowAPI "FlowAPI は、アクション、フロー、またはサブフローを、ブロッキングメソッドまたはノンブロッキングメソッドのいずれかを使用してサーバー側スクリプトで実行するメソッドを提供します。")」を参照してください。  
注:  
デフォルトでは、1 ページのデータを MID サーバー から取得する際にインスタンスは 600 秒間待機します。MID サーバー で データストリーム アクションを実行しているときにタイムアウトが発生した場合は、datastream_alternative_env_fetch_page_timeout_seconds システムプロパティを増やしてこのデフォルトを変更します。
{#ScriptableDS-next__table_lrc_f43_bjb__entry__3}

| 名前 | タイプ | 説明 |
|-|-|-|
| なし |   |   |
[表 : 11. パラメーター]

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

| タイプ | 説明 |
|-|-|
| オブジェクト | データストリーム内の次のアイテム。このオブジェクトには、データストリームアクションによって定義された出力が含まれます。データストリームアクションの出力を表示するには、Flow Designer インターフェイスのデータストリームアクションの \[出力\] セクションに移動します。 |
[表 : 12. 返される内容]

{#ScriptableDS-next__table_mrc_f43_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 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();
    	}
    	
    })();


