ServiceNow Fluent と ServiceNow SDK を使用してコードでアプリケーションメタデータを定義する
ServiceNow SDK および コードでアプリケーションメタデータを定義するServiceNow Fluent。
始める前に
- ServiceNow Fluent 言語サーバーをインストールして、Visual Studio Code の ServiceNow Fluent の言語処理と検証を取得します。詳細については、「Visual Studio Code に ServiceNow Fluent 言語サーバーをインストールする」を参照してください。
- ServiceNow SDKを使用してスコープ対象のアプリケーションを作成または変換します。詳細については、 を使用してアプリケーションを作成 する ServiceNow SDK または を参照してください。
必要なロール:admin
このタスクについて
ServiceNow Fluent は、アプリケーションを構成するメタデータファイル [sys_metadata] を定義するための TypeScript に基づくドメイン固有言語 (DSL) であり、テーブル、ロール、ACL、ビジネスルール、自動テストフレームワーク (ATF) テストなど、さまざまなタイプのメタデータの API が含まれています。 ServiceNow Fluent API と例の詳細については、「ServiceNow Fluent API リファレンス」を参照してください。
手順
ヒント:
コードコメントで次のディレクティブを使用して、コードを管理できます。
@fluent-ignore:次のコード行 ServiceNow Fluent 診断の警告とエラーを抑制します。@fluent-disable-sync: ServiceNow Fluent オブジェクトへの変更の同期をオフにします。呼び出し式の前 (例: Record({ ... }))を使用して、そのオブジェクトとその子オブジェクトの同期をオフにします。このディレクティブは、ソースコードの外部でオブジェクトに加えられた変更を無視し、同期時に更新しない場合にのみ使用してください。@fluent-disable-sync-for-file: ServiceNow Fluent ファイル (.now.ts) への変更の同期をオフにします。ファイルの最初の行で使用して、ファイル内のすべてのコードの同期をオフにします。このディレクティブは、ソースコードの外部でファイルに加えられた変更を無視し、同期時に更新しない場合にのみ使用してください。
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,
})アプリケーションをビルドて展開した後、このソースコードはインスタンスに次のアプリケーションメタデータファイルを生成します。