ServiceNow FluentServiceNow SDK を使用してコードでアプリケーションメタデータを定義する

  • リリースバージョン: Xanadu
  • 更新日 2024年08月01日
  • 所要時間:7分
  • を使用してコードでアプリケーションメタデータを定義する ServiceNow Fluent そして ServiceNow SDK

    始める前に

    必要なロール:admin

    このタスクについて

    ServiceNow Fluent は、アプリケーションを構成するメタデータファイル [sys_metadata] を定義するための TypeScript に基づくドメイン固有言語 (DSL) であり、テーブル、ロール、ACL、ビジネスルール、 自動テストフレームワーク (ATF) テストなど、さまざまなタイプのメタデータの API が含まれています。 ServiceNow Fluent API と例の詳細については、「ServiceNow Fluent API リファレンス」を参照してください。

    手順

    1. Visual Studio Code で、スコープ対象のアプリケーションのディレクトリを開きます。
    2. src/fluent ディレクトリに、拡張子が .now.ts のファイルを追加します。
      アプリケーション メタデータは、1 つのファイルまたは必要な数の .now.ts ファイルで定義し、 fluent ディレクトリ内のディレクトリにファイルを整理できます。
      ヒント:
      fluent ディレクトリでは、index.now.ts という名前のサンプル ファイルを参照できます。
    3. .now.ts ファイルで、ServiceNow Fluent API のオブジェクトを使用して、アプリケーションのメタデータを定義します。
      たとえば、アプリケーションでテーブル [sys_db_object] を作成するには、テーブル API を使用します。次の例では、必要なプロパティと値を持つ Table オブジェクトを使用して、To Do リスト用のシンプルなテーブルを作成しています。
      export const x_snc_example_to_do = Table({
          name: 'x_snc_example_to_do',
          schema: {
              deadline: DateColumn({ label: 'deadline' }),
              state: StringColumn({
                  label: 'State',
                  choices: {
                      ready: { label: 'Ready' },
                      completed: { label: 'Completed' },
                      in_progress: { label: 'In Progress' },
                  },
              }),
              task: StringColumn({ label: 'Task', maxLength: 120 }),
          },
      })
      注:
      レコード API を使用して、専用 API を持たないアプリケーションメタデータを定義できます。
    4. @servicenow/sdk/core から API に必要なインポートを追加します。
      たとえば、日付列と文字列列を持つテーブルを定義するには、使用する対応するオブジェクトをテーブル API からインポートします。
      import { Table, DateColumn, StringColumn } from '@servicenow/sdk/core'
    5. オプション: ビジネスルールなどのサーバー側スクリプトを使用する ServiceNow Fluent API の場合は、JavaScript モジュールからコードをインポートして、オブジェクトのスクリプトプロパティから呼び出します。
      この例では、スクリプトプロパティから参照できる showStateUpdate 関数をインポートします。
      import { showStateUpdate } from '../server/script.js'
    6. 変更内容を保存します。

    ServiceNow Fluent を使用してソースコードでアプリケーションメタデータを定義する

    拡張子が .now.ts のファイルで、ServiceNow Fluent API のオブジェクトを使用して、アプリケーションのメタデータを定義します。また、@servicenow/sdk/core からの API に、必要なインポートも含める必要があります。

    次の例には、アプリケーションのテーブル、クライアントスクリプト、およびビジネスルールの定義が含まれています。ビジネスルールは、script.js JavaScript モジュールの関数を使用します。
    import '@servicenow/sdk/global'
    import { BusinessRule } from '@servicenow/sdk/core'
    import { ClientScript } from '@servicenow/sdk/core'
    import { Table, DateColumn, StringColumn } from '@servicenow/sdk/core'
    import { showStateUpdate } from '../server/script.js'
    
    //creates todo table, with three columns (deadline, status and task)
    export const x_snc_example_to_do = Table({
        name: 'x_snc_example_to_do',
        schema: {
            deadline: DateColumn({ label: 'deadline' }),
            state: StringColumn({
                label: 'State',
                choices: {
                    ready: { label: 'Ready' },
                    completed: { label: 'Completed' },
                    in_progress: { label: 'In Progress' },
                },
            }),
            task: StringColumn({ label: 'Task', maxLength: 120 }),
        },
    })
    
    //creates a client script that pops up 'Table loaded successfully!!' message everytime todo record is loaded
    ClientScript({
        $id: Now.ID['cs0'],
        name: 'my_client_script',
        table: 'x_snc_example_to_do',
        active: true,
        applies_extended: false,
        global: true,
        ui_type: 'all',
        description: 'Custom client script generated by Now SDK',
        messages: '',
        isolate_script: false,
        type: 'onLoad',
        script: script`function onLoad() {
            g_form.addInfoMessage("Table loaded successfully!!")
        }`,
    })
    
    //creates a business rule that pops up state change message whenever a todo record is updated
    BusinessRule({
        $id: Now.ID['br0'],
        action: ['update'],
        table: 'x_snc_example_to_do',
        script: showStateUpdate,
        name: 'LogStateChange',
        order: 100,
        when: 'after',
        active: true,
    })

    アプリケーションをビルドて展開した後、このソースコードはインスタンスに次のアプリケーションメタデータファイルを生成します。

    図 : 1. ServiceNow Fluent コードから生成されたアプリケーションメタデータ
    コード例から生成されたアプリケーションファイル。

    次のタスク

    アプリケーションをビルドおよび展開して、ソースコードをアプリケーションメタデータにコンパイルして、変更をインスタンスで使用できるようにします。詳細については、「ServiceNow SDK でアプリケーションをビルドして展開する」を参照してください。