リモートテーブルのスクリプト定義の作成

  • リリースバージョン: Australia
  • 更新日 2026年03月12日
  • 所要時間:11分
  • 外部データソースからデータを取得したり、外部ソースのデータを編集したりするために、 リモートテーブルのスクリプトを定義します。

    始める前に

    必要なロール:スクリプティング許可権限がある delegated_developer または admin

    このタスクについて

    ServiceNow AI Platform は、外部データソースに対してクエリスクリプトを実行することによってリモートテーブルのデータを取得してキャッシュし、ユーザーがデータを表示または変更できるようにします。また、この外部データのキャッシュ方法と、スクリプトを再実行するまでにメモリにデータをキャッシュしておく期間を指定することもできます。

    手順

    1. 移動先 すべて > システム定義 > リモートテーブル > 定義.
    2. [New (新規)] を選択します。
    3. フォームのフィールドに入力します。
      コントロール 説明
      名前

      選択したリモートテーブルに関連付けるために作成するスクリプトの名前。

      テーブル
      作成しているスクリプトを関連付けるリモートテーブルの名前。
      • スコープ対象のアプリケーションのリモートテーブルの場合、その名前にはリモートでアプリケーションの一部であることを示す名前空間識別子と文字列 st_ がプリフィックスとして付きます。
      • グローバルアプリケーションのリモートテーブルの場合、名前には文字列 u_st_ がプリフィックスとして付きます。
      アプリケーション このリモートテーブルに関連付けられるアプリケーション。アプリケーションで作業している場合、またはアプリケーションレコードからリモートテーブルを作成している場合、フィールドはデフォルトでそのアプリケーションに設定されます。それ以外の場合は、このフィールドのデフォルトは [グローバル] です。モジュールやセキュリティルールなど、テーブルレコードから作成されたすべてのレコードは、デフォルトでこのアプリケーションに割り当てられます。
      アクティブ リモートテーブルに関連付けるスクリプトをアクティブ化するためのオプション。リモートテーブルに対して複数の非アクティブなスクリプト定義を設定できますが、アクティブなスクリプト定義は 1 つのみです。スクリプト定義をアクティブ化すると、外部データを取得してキャッシュできるように、それをリモートテーブルに関連付けます。
      詳細 キャッシュと編集の詳細設定にアクセスするためのオプション。
      [キャッシュ] の設定は、このリモートテーブルの ServiceNow AI Platform でメモリキャッシュをどのように動作させるかを指定します。
      注:
      上級ユーザーのみがリモートテーブルのキャッシュパラメーターを設定する必要があります。
    4. オプション: [詳細] オプションを選択した場合は、[詳細] セクションでフィールドに入力します。
      注:
      デフォルトでは、外部データはユーザーによってメモリにキャッシュされます。
      フィールド 説明
      キャッシュ TTL このリモートテーブルスクリプト定義のために外部データがメモリにキャッシュされるキャッシュ存続時間 (秒単位)。たとえば、メモリにデータを 5 分間キャッシュするには、300 と入力します。

      デフォルト値は 0 です。つまり、データはキャッシュされず、毎回取得されます。最大値は 60 分 (3600 秒) です。

      キャッシュ分離レベル ユーザーごとまたはすべてのユーザーのクエリ結果をキャッシュします。
      • ユーザーごとのキャッシュ:キャッシュされるクエリ結果は、ログインしているユーザーに固有のものである必要があります。これがデフォルト値です。
      • システム共有キャッシュ:キャッシュされるクエリ結果は、すべてのユーザーに対して同じである必要があります。
      拡張されたキャパシティ リモートテーブル内の 1000 行を超える行をサポートするように拡張されたキャパシティを選択するオプション。
      注:
      外部ソースから取得されるデータのサイズが小さい場合は、リモートテーブルで 1000 行のデフォルト制限を使用して、クエリ時間を最短にします。取得したデータのサイズが大きく、パフォーマンスが低下する可能性がある場合は、[ 拡張されたキャパシティ ] フィールドを選択できます。

      [ 編集可能 ] が選択されている場合、このフィールドはサポートされません。

      編集可能 インスタンスからのリモートテーブルとその外部データソースの編集をサポートするオプション。v_record API を使用して、テーブルでデータを挿入、更新、および削除するためのスクリプト定義をカスタマイズします。
    5. v_tablev_query、および v_record API を使用して、次のスクリプト定義をカスタマイズします。

      これらの API の詳細については、「v_table – Scoped, Global」、「v_query – Scoped, Global」、「v_record - Scoped, Global」を参照してください。

      スクリプト定義 説明
      クエリ

      クエリスクリプト定義は、リモートテーブルに含めるデータを外部ソースから取得します。[クエリ] セクションで、v_table API と v_query API を使用してスクリプトをカスタマイズします。

      リモートテーブルの外部データを含むリストを更新するたびに、関連するクエリスクリプトが実行されます。

      (function executeQuery(v_table, v_query) {
           // Parameters:
           //      v_table - object to record response:
           //          v_table.addRow({ ... })                - adds a row to the result set.
           //              keys are the column names of the table definition.
           //              sys_id must be non-empty and uniquely identify each row.
           //                  the above rule will be strictly enforced for editable tables, and generate warnings for read-only tables.
           //                  sys_id will be supplied to the Update, and Delete scripts to specify the row's identity.
       
           //      v_query - the query definition:
           //          v_query.getEncodedQuery()              - encoded querystring
           //          v_query.getCondition(field)            - encoded querystring for the given field (includes field name, operator, and value)
           //          v_query.getParameter(field)            - parameter for the given field (only includes value for equality conditions)
           //          v_query.isGet()                        - whether the query is a single get by sys_id
           //          v_query.getSysId()                     - parameter for sys_id field
           //          v_query.isTextSearch()                 - whether the query contains a text query parameter
           //          v_query.getTextSearch()                - text search query parameter (internal field name 123TEXTQUERY321)
           //          v_query.getFirstRowWanted()            - the first row to include, don't use with caching
           //          v_query.getLastRowWanted()             - the last row to include, don't use with caching
           //          v_query.setLastErrorMessage(message)   - the message is visible from GlideRecord.getLastErrorMessage()
       
           // Sample code:
           //  try {
           //      retrieve-rows-from-external-system;     //      if (there-was-an-error) {
           //          var message = ...;     //          v_query.setLastErrorMessage(message);
           //          return;
           //      }
           //
           //      for (var row in rows) {
           //          v_table.addRow(row);
           //      }
           //  } catch (ex) {
           //      var message = ex.getMessage();
           //      v_query.setLastErrorMessage(message);
           //  }
       
        })(v_table, v_query);
      挿入

      挿入スクリプト定義は、外部ソースにレコードを追加します。このスクリプト定義は、高度な [編集可能] オプションを選択した場合にのみ使用できます。[挿入] セクションで、v_record API を使用してスクリプトをカスタマイズします。

      リモートテーブルにレコードが追加されるたびに、関連する挿入スクリプトが実行されます。

      function executeInsert(v_record) {
           // Parameters:
           //      v_record is a map of field names and values containing the sys_id of the
           //              record and the fields that need to be inserted in the record on the
           //              remote system (source of data)
           
           //          v_record.<field_name>                   - fields in the remote table GlideRecord
           //          v_record.setLastErrorMessage(message)   - signal an error
       
           // Sample code:
           //  try {
           //      update-external-system;
           //      if (there-was-an-error) {
           //          var message = ...;
           //          v_record.setLastErrorMessage(message);
           //      }
           //  } catch (ex) {
           //      var message = ex.getMessage();
           //      v_record.setLastErrorMessage(message);
           //  }
        })(v_record);
      更新

      更新スクリプト定義は、外部ソースでレコードを変更します。このスクリプト定義は、高度な [編集可能] オプションを選択した場合にのみ使用できます。[更新] セクションで、v_record API を使用してスクリプトをカスタマイズします。

      リモートテーブルでレコードが更新されるたびに、関連する更新スクリプトが実行されます。

      注:
      複数のユーザーが同じレコードを同時に更新する場合、リモートシステム上の値は最後に実行された更新呼び出しの値になります。
      (function executeUpdate(v_record, v_changed_fields) {
           // Parameters:
           //    v_record - a map of field names and values containing the sys_id of the record
           //          v_record.<field_name>                   - fields in the remote table GlideRecord
           //          v_record.setLastErrorMessage(message)   - signal an error
           //    v_changed_fields - a map of field names and values containing the sys_id of the record
           //          v_changed_fields.<field_name>           - changed fields in the remote table GlideRecord
       
           // Sample code:
           //  try {
           //      update-external-system;
           //      if (there-was-an-error) {
           //          var message = ...;
           //          v_record.setLastErrorMessage(message);
           //      }
           //  } catch (ex) {
           //      var message = ex.getMessage();
           //      v_record.setLastErrorMessage(message);
           //  }
        })(v_record, v_changed_fields);
      削除

      削除スクリプト定義は、外部ソースからレコードを削除します。このスクリプト定義は、高度な [編集可能] オプションを選択した場合にのみ使用できます。[削除] セクションで、v_record API を使用してスクリプトをカスタマイズします。

      リモートテーブルからレコードが削除されるたびに、関連する削除スクリプトが実行されます。

      (function executeDelete(v_record) {
           // Parameters:
           //      v_record - a map of field names and values containing (among others) the
           //              sys_id of the record that needs to be deleted on the remote system
           //          v_record.<field_name>                  - fields in the remote table GlideRecord
           //          v_record.setLastErrorMessage(message)   - signal an error
       
           // Sample code:
           //  try {
           //      update-external-system;
           //      if (there-was-an-error) {
           //          var message = ...;
           //          v_record.setLastErrorMessage(message);
           //      }
           //  } catch (ex) {
           //      var message = ex.getMessage();
           //      v_record.setLastErrorMessage(message);
           //  }
        })(v_record);
    6. [送信] を選択します。