データソーススクリプトの例

  • リリースバージョン: Yokohama
  • 更新日 2025年03月05日
  • 所要時間:12分
  • データソーススクリプトを使用して、入力値、説明要素、および入力アクションをマッピングします。または、これらの要素ごとに専用のデータソースを作成します。

    データソーススクリプトは、 valuesMappercontext の 2 つのパラメーターを受け入れます。コンテキストパラメーターは、ユーザーが操作している入力フォームを参照します。valuesMapper パラメーターは、addRecordMapping メソッドを使用して要素識別子を特定のテーブル列にリンクする役割を担います。たとえば、メソッド呼び出しは次のようになります。
    valuesMapper.addRecordMapping(UNIQUE_ELEMENT_IDENTIFIER, GLIDE_RECORD_INSTANCE, COLUMN_NAME);
    注:
    このトピックにリストされているすべてのスクリプトは、データソースレコード内で構成されます。詳細については、「データソースの構成」を参照してください。

    デフォルトのサンプルスクリプト

    モバイルアプリビルダー内で提供されているサンプルスクリプトを次に示します。スクリプトは、既存のテーブルをフィルタリングする方法と、特定のフィールドを UI 要素にマップする方法を示すテンプレートを提供します。
    (function DataSource(valuesMapper, context) {
    var gr = new GlideRecord(TABLE_NAME); // Could be any table within the Instance, including custom tables
    gr.addQuery('sys_id',context.getUniqueValue());
    gr.query();
    if (gr.next()) { // Could also be itetaring muliple records from the same table using the 'while' iterator
    
        valuesMapper.addRecordMapping('short_description',gr,'short_description'); // Map the field short_description to a UI element that has element identifier of "short_description". The element identifier could be any String, it's easier to map the column's name as the element identifer.
    
        valuesMapper.addRecordMapping('priority',gr,'priority'); // Map the field priority to a UI element that has element identifier of "priority". The element identifier could be any String, it's easier to map the column's name as the element identifier.
    
    }
    })(valuesMapper, context);
    
    このサンプル スクリプトには次のものが含まれます。
    • データソースを定義する次のパラメーター:
      • valuesMapper:このパラメーターは、データソースから UI 要素に値をマッピングするために使用されます。
      • コンテキスト:このパラメーターは、インシデント、変更要求、その他のテーブルなど、データソースが使用されているコンテキストを指定します。
    • addRecordMapping メソッドは、フィールドをマッピングするために使用され、次のパラメーターで構成されます。
      • 要素識別子:マッピングするフィールドに定義した一意の名前。この例では、これは <簡単な説明> で、 <priority> です。
      • Glide レコード <gr>:これは、カスタムテーブルを含むインスタンス内の任意のテーブルを参照するために使用されます。
      • フィールド名:これは、Glide レコード内のフィールドの名前です。この例では、これは <簡単な説明> で、 <priority> です。
        注:
        一般的なガイドラインでは、要素の識別子にフィールド名と同じ名前を付けて、コードの保守と理解を容易にします。

    入力フォームに設定された入力をマッピングするスクリプト

    属性 [sys_sg_input_attribute] テーブルで構成された入力の ElementIdentifier 属性がどのように処理されるかを示すスクリプトの例を次に示します。このスクリプトには、 input_1input_2input_3input_4input_5 という名前の 5 つの入力を含むインシデントテーブルの列に入力値をマッピングする例が含まれています。

    (function DataSource(valuesMapper, context) {
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id',context.getUniqueValue());
    gr.query();
    if (gr.next()) {
    // Map the column short_description from the incident table to the elementIdentifier "input_1_short_description".
    valuesMapper.addRecordMapping('input_1_short_description', gr,'short_description'); 
    
    // Map the column start_time from the incident table to the elementIdentifier "input_2_start_time".
    valuesMapper.addRecordMapping('input_2_start_time',gr,'start_time'); 
    
    // Map the column score from the incident table to the elementIdentifier "input_3_score".
    valuesMapper.addRecordMapping('input_3_score', gr,'score'); 
    
    // Map the column assigned_to from the incident table to the elementIdentifier "input_4_assigned_to".
    valuesMapper.addRecordMapping('input_4_assigned_to',gr,'assigned_to');
    
    // Map the attachment from the record in the incident table to the elementIdentifier "input_5_attachment_1" (The column name must be “sys_id”).
    valuesMapper.addRecordMapping('input_5_attachment_1',gr,'sys_id');
    
     }
    })(valuesMapper, context);
    
    このコードは、インシデント [incident] テーブルレコードの特定の列を対応する要素識別子にマップする関数 DataSource を定義します。次のプロセスが発生します。
    • インシデント [incident] テーブルの GlideRecord オブジェクトを作成します。
    • コンテキストの一意の値を使用して、特定のsys_idを持つレコードのテーブルをクエリします。
    • レコードが見つかると、次の列が相関する一意の要素識別子にマップされます。
      • input_1_short_description short_description
      • input_2_start_time start_time
      • input_3_scoreするスコア
      • input_4_assigned_to assigned_to
      • input_5_attachment_1 sys_id

    入力フォームの入力または入力セクションに関連付けられた説明要素をマッピングするスクリプト

    以下は、説明要素 [sys_sg_descriptive_element] テーブルで構成された説明要素の ElementIdentifier 属性を処理する方法を示すスクリプトの例です。
    (function DataSource (valuesMapper, context) {
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id',context.getUniqueValue());
    gr.query();
    if (gr.next()) {
    // Handle descriptive element of type Rich Text. Map to a column named "rich_text_1_col" in the incident table to the elementIdentifier "input_1_rich_text_descriptive_element_id". The col type in the "incident" should be "HTML"
    valuesMapper.addRecordMapping('input_1_rich_text_descriptive_element_id',gr,'rich_text_1_col'); 
    
    // Handle descriptive element of type Text. Map to a column named "plain_text_1_col" in the incident table to the elementIdentifier "input_1_text_descriptive_element_id". The col type in the "incident" should be "String"/"String (Full UTF-8)"
    valuesMapper.addRecordMapping('input_1_text_descriptive_element_id',gr,'plain_text_1_col'); 
    
    // Handle descriptive element of type Image. Map to a column named "image_1_col" in the incident table to the elementIdentifier "input_1_image_descriptive_element_id". The col type in the "incident" should be "Image"
    valuesMapper.addRecordMapping('input_1_image_descriptive_element_id',gr,'image_1_col'); 
    }
    })(valuesMapper, context);
    
    このコードは、インシデント [incident] テーブルレコードの特定の列を対応する要素識別子にマップする関数 DataSource を定義します。次のプロセスが発生します。
    • インシデント [incident] テーブルの GlideRecord オブジェクトを作成します。
    • コンテキストの一意の値を使用して、特定のsys_idを持つレコードのテーブルをクエリします。
    • レコードが見つかると、次の列が相関する一意の要素識別子にマップされます。
      • input_1_rich_text_descriptive_element_idするrich_text_1_col (HTML タイプ)
      • input_1_text_descriptive_element_idするplain_text_1_col (文字列タイプ)
      • image_1_col (イメージタイプ) から input_1_image_descriptive_element_id

    入力フォームの入力に関連付けられた入力アクションをマッピングするスクリプト

    入力フォームアクション [sys_sg_parameter_action] テーブルで構成された入力アクションの ElementIdentifier 属性を処理する方法を示すスクリプトの例を次に示します。
    (function DataSource(valuesMapper, context) {
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id',context.getUniqueValue());
    gr.query();
    if (gr.next()) {
    // Handle an attachment type input action. Map the elementIdentifier "input_1_action_attachments" to a record in the incident table by specifying the sys_id as the column in the table.
    valuesMapper.addRecordMapping('input_1_action_attachments',gr,'sys_id'); 
    
    // Handle a comment type input action. Map the elementIdentifier "input_1_action_comment" to a column in the incident table where the comment is stored. In this example, the comment is stored in the //"comment_by_agent" column.
    valuesMapper.addRecordMapping('input_1_action_comment',gr,'comment_by_agent'); 
    
    //Handle a navigation type input action with a record context.
    Map the elementIdentifier "input_1_navigation" to a record in the incident table by specifying the sys_id as the column in the table.
    valuesMapper.addRecordMapping('input_1_navigation',gr,'sys_id'); 
    }
    })(valuesMapper, context);
    
    このコードは、インシデント [incident] テーブルレコードの特定の列を対応する要素識別子にマッピングする関数 DataSource を定義します。次のプロセスが発生します。
    • [インシデント] テーブルの GlideRecord オブジェクトを作成します。
    • コンテキストの一意の値を使用して、特定のsys_idを持つレコードのテーブルをクエリします。
    • レコードが見つかると、次の列が相関する一意の要素識別子にマップされます。
      • 添付ファイルタイプの入力アクションを処理するためのsys_id input_1_action_attachments
      • コメントタイプの入力アクションを処理するためのinput_1_action_comment comment_by_agent
      • レコードコンテキストを使用してナビゲーションタイプの入力アクションを処理するためのinput_1_navigation sys_id