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

  • リリースバージョン: Washingtondc
  • 更新日 2024年02月01日
  • 読む11読むのに数分
  • ScriptableDataStream API は、データのストリームを操作するためのメソッドを提供します。

    このクラスは、次のいずれかの API を使用して ScriptableDataStream オブジェクトを取得した後のサーバー側スクリプトでのみ使用できます。

    • FlowAPI クラスの executeDataStreamAction() メソッド。「FlowAPI」を参照してください。
    • ScriptableFlowRunnerResult クラスの getDataStream() メソッド。「ScriptableFlowRunnerResult」を参照して下さい。

    ScriptableDataStream オブジェクトを取得した後、次の順序でメソッドを呼び出します。

    1. hasNext() メソッドを使用して、データストリーム内にまだアイテムがあるかどうかを判断します。
    2. next() メソッドを使用して、ストリーム内の次のアイテムにアクセスします。
    3. getItemIndex()getItemInPageIndex()、および getPageIndex() メソッドを使用して、ストリームから情報を取得します。
    4. close() メソッドを使用してストリームを閉じます。
    注:
    エラーを捕捉するために、常にデータストリームのロジックを try/catch ブロックで囲みます。データストリームを閉じてパフォーマンスの問題を防ぐために、常に、ScriptableDataStream クラスの close() メソッドで終了する finally ステートメントを含めます。

    ScriptableDataStream - close()

    データストリームへの接続を閉じます。データストリームで必要な操作を実行した後は、常にこのメソッドを呼び出します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

    表 : 1. パラメーター
    名前 タイプ 説明
    なし
    表 : 2. 返される内容
    タイプ 説明
    なし
    (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()

    データストリーム内のアイテムの現在のインデックスを返します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

    表 : 3. パラメーター
    名前 タイプ 説明
    なし
    表 : 4. 返される内容
    タイプ 説明
    数字 ゼロベースのインデックスを使用したデータストリーム内のアイテムの現在のインデックス。
    (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()

    データストリームの現在のページにあるアイテムの現在のインデックスを返します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

    表 : 5. パラメーター
    名前 タイプ 説明
    なし
    表 : 6. 返される内容
    タイプ 説明
    数字 ゼロベースのインデックスを使用したデータストリームの現在のページにあるアイテムの現在のインデックス。
    (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()

    データストリーム内のページの現在のインデックスを返します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

    表 : 7. パラメーター
    名前 タイプ 説明
    なし
    表 : 8. 返される内容
    タイプ 説明
    数字 ゼロベースのインデックスを使用したデータストリーム内のページの現在のインデックス。
    (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()

    データストリームにさらにアイテムがある場合は true を返します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

    注:
    表 : 9. パラメーター
    名前 タイプ 説明
    なし
    表 : 10. 返される内容
    タイプ 説明
    ブーリアン データストリーム内にまだアイテムがあるかどうかを判断するフラグ。次の値が含まれます。
    • true:データストリーム内で反復するアイテムがまだある。
    • false:データストリームにこれ以上アイテムはない。

    この例は、データストリームで返された各アイテムのインシデントレコードを作成します。

    (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()

    データストリーム内の次のアイテムを返します。

    このメソッドは、FlowAPI クラスの executeDataStreamAction() メソッドから返された ScriptableDataStream オブジェクトに対してのみ呼び出すことができます。「FlowAPI」を参照してください。

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

    この例は、データストリームで返された各アイテムのインシデントレコードを作成します。

    (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();
    	}
    	
    })();