例 1:外部ソースからすべてのインシデントレコードを取得する

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:9分
  • これらは、現在のインスタンスの外部のソースからデータを取得およびキャッシュするために作成できるスクリプト定義の例です。この最初の例では、外部ソースからすべてのインシデントレコードをロードするスクリプトを作成します。

    リモートテーブル API 情報については、次を参照してください。
    /**
     * Using `v_query`, add the rows to `v_table`
     */
    (function executeQuery(v_table, v_query) {
    ​
    	fetchAllIncidents(v_table, v_query);
    ​
    	/**
    	 * fetch all incidents records from the remote instance
    	 */
    	function fetchAllIncidents(v_table, v_query) {
    		// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
    		// Create a RestMessage first which calls an external REST service
    		try {
    			var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');
    			var response = restMessage.execute();
    			var responseBody = response.getBody();
    			
    			// if REST call ends up in an error, set the last error message which shows up
    			// at the bottom of the list view
    			if (response.haveError()) {
    				v_query.setLastErrorMessage(response.getErrorMessage());
    				// can use gs.error() or gs.addErrorMessage() while debugging
    				// gs.debug() messages visible in session debugger
    				// gs.debug(response.getErrorMessage());
    				return;
    			}
    		} catch (ex) {
    			v_query.setLastErrorMessage(ex.message);
    			// gs.debug(ex.message);
    			return;
    		}
    ​
    		var transformerDefinition = getTransformerDefinition();
    		var transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);
    		// transformer parses the responseBody and extracts rows
    		while (transformer.transform()) {
    			// row is field-value map e.g. { active:"true", number: "INC0000001"}
    			var row = transformer.getRow();
    			// you may do any additional transformations to the row like GlideDuration, GlideDataTime etc. For example,
    			// row.duration = new GlideDuration(row.duration);// finally add the row to the remote table
    			v_table.addRow(row);
    		}
    	}
    ​
    	/**
    	 * returns a sn_tfrm.TransformerDefinition, which defines the mapping of the table fields and elements in the response body
    	 */
    	function getTransformerDefinition() {
    		// create a rule list to map a field to its element path
    		var ruleList = new sn_tfrm.TransformerRuleList()
    			.fromJSON() // the response body is a JSON
    			// 'active' field maps to path '$.active'
    			.addRule("active", "$.active")
    			.addRule("caller_id", "$.caller_id.value")
    			.addRule("number", "$.number")
    			.addRule("short_description", "$.short_description")
    			.addRule("sys_id", "$.sys_id")
    			.addRule("updates", "$.sys_mod_count");
    ​
    		var recordPath = "$.result";
    		return new sn_tfrm.TransformerDefinition(ruleList, recordPath);
    	}
    	
    })(v_table, v_query);

    このスクリプトでは、次のコードスニペットを確認してください。

    function fetchAllIncidents(v_table, v_query) {
    		// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'
    		// Create a RestMessage first which calls an external REST service
    		try {
    			var restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');
    			var response = restMessage.execute();
    			var responseBody = response.getBody();
    			
    			// if REST call ends up in an error, set the last error message which shows up
    			// at the bottom of the list view
    			if (response.haveError()) {
    				v_query.setLastErrorMessage(response.getErrorMessage());
    				// can use gs.error() or gs.addErrorMessage() while debugging
    				// gs.debug() messages visible in session debugger
    				// gs.debug(response.getErrorMessage());
    				return;
    			}
    		} catch (ex) {
    			v_query.setLastErrorMessage(ex.message);
    			// gs.debug(ex.message);
    			return;
    		}

    RestMessage を作成して、スクリプトでこれを直接使用することができます。この例では、リモートインスタンスインシデントという名前の RESTMessageV2 API と、すべてのインシデントデータを取得する関数 All Incidents を使用しています。サーバーから応答が返されると、データ取得で問題が発生した場合にエラーメッセージが表示されます。

    注:
    RESTMessageV2 の使用方法とダイレクトメッセージの定義方法の詳細については、「RESTMessageV2 - Scoped, Global (RESTMessageV2 - スコープ対象、グローバル)」および「Direct RESTMessageV2 example (直接 RESTMessageV2 の例)」を参照してください。

    データ取得で問題が発生しなかった場合は、レコードのデータ本文を取得します。

    		var transformerDefinition = getTransformerDefinition();
    		var transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);
    		// transformer parses the responseBody and extracts rows
    		while (transformer.transform()) {
    			// row is field-value map e.g. { active:"true", number: "INC0000001"}
    			var row = transformer.getRow();
    			// you may do any additional transformations to the row like GlideDuration, GlideDataTime etc. For example,
    			// row.duration = new GlideDuration(row.duration);
    ​
    			// finally add the row to the remote table
    			v_table.addRow(row);
    

    次に、Transformer API を使用して必要なデータ変換を実行し、行を抽出して、各レコードの行をリモートテーブルに追加します。

    /**
    	 * returns a sn_tfrm.TransformerDefinition, which defines the mapping of the table fields and elements in the response body
    	 */
    	function getTransformerDefinition() {
    		// create a rule list to map a field to its element path
    		var ruleList = new sn_tfrm.TransformerRuleList()
    			.fromJSON() // the response body is a JSON
    			// 'active' field maps to path '$.active'
    			.addRule("active", "$.active")
    			.addRule("caller_id", "$.caller_id.value")
    			.addRule("number", "$.number")
    			.addRule("short_description", "$.short_description")
    			.addRule("sys_id", "$.sys_id")
    			.addRule("updates", "$.sys_mod_count");
    ​
    		var recordPath = "$.result";
    		return new sn_tfrm.TransformerDefinition(ruleList, recordPath);
    	}
    	
    })(v_table, v_query);
    
    getTransformerDefinition は、外部 API 応答本文でレコードのスキーマを定義します。テーブルスクリプトの各フィールドを外部レコードの要素にマップします。このマッピング外の外部データ要素はサポートも取得もされません。
    注:
    トランスフォーマー定義の sys_id を外部データの要素にマップする必要があります。この場合、sys_id は外部インシデント sys_id にマップされます。sys_id の最大長は 32 文字です。この sys_id マッピングは、外部データを使用するフォームが適切に動作するように行います。