---
sourceDocument: オーストラリア ServiceNow AI Platform の機能
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/servicenow-platform

 Release :

    - australia

ft:locale :

    - ja-JP

ft:publication_title :

    - オーストラリア ServiceNow AI Platform の機能

ft:clusterId :

    - platcap

bundleId :

    - platcap

workflow :

    - Platform


---

# 例 1

# 例 1：外部ソースからすべてのインシデントレコードを取得する {#ariaid-title1}

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

これらは、現在のインスタンスの外部のソースからデータを取得およびキャッシュするために作成できるスクリプト定義の例です。この最初の例では、外部ソースからすべてのインシデントレコードをロードするスクリプトを作成します。
リモートテーブル API 情報については、次を参照してください。

* [v_query -- Scoped, Global](https://www.servicenow.com/docs/access?context=v_queryAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)
* [v_record - Scoped, Global](https://www.servicenow.com/docs/access?context=v_recordAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)
* [v_table -- Scoped, Global](https://www.servicenow.com/docs/access?context=v_tableAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)
{#remote-table-script-def-example1__vtable-api-xbl-list}

    /**
    * 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 - スコープ対象、グローバル)](https://www.servicenow.com/docs/access?context=c_RESTMessageV2API&version=australia&pubname=australia-api-reference&ft:locale=en-US)」および「[Direct RESTMessageV2 example (直接 RESTMessageV2 の例)](https://www.servicenow.com/docs/access?context=r_DirectRESTMessageV2Example&version=australia&pubname=australia-api-reference&ft:locale=en-US)」を参照してください。

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

    		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 マッピングは、外部データを使用するフォームが適切に動作するように行います。
**関連資料**   

* [リモートテーブルスクリプト定義のデバッグ](https://servicenow-prod.fluidtopics.net/nzidBrYNd7sGtGFZhnSMbQ "リモートテーブルスクリプト定義のセッションデバッグを有効にできます。セッションデバッグログでスクリプト定義のログ記録を有効にするには、glide.script.vtable.log.debug プロパティを true に設定します。")  
**関連情報**   

* [TransformerDefinition API-スコープ、グローバル](https://www.servicenow.com/docs/access?context=TransformerDefinitionAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)
* [TransformerRuleList API - Scoped, Global](https://www.servicenow.com/docs/access?context=TransformerRuleListAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)
* [TransformerScripted API - Scoped, Global](https://www.servicenow.com/docs/access?context=TransformerScriptedAPI&version=australia&pubname=australia-api-reference&ft:locale=en-US)

