2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ServiceNow Flow Designerで複数のレコード内容を処理する

Posted at

概要

ServiceNowのFlow Designerで短時間でフローを定義することはできますが、グローバル変数の概念がないため、標準Actionでは複数レコードの内容を一つの変数に纏められません。しかし、カスタムActionでレコードを処理するようにしたらすることが可能です。

次のようなフローを作成します。「For Each Item in」はログ出力用のActionなので、実際のフローには不要です。
flow_1.png

Action

先ずはフローで利用するカスタムアクションを作成します。
1.例ではメインフローでIncidentレコードを抽出しているのでActionには複数のIncidentレコードが引き渡されます。Typeは「Records.incident」になります。
action_1.png
2.スクリプトは次のようになります。WhileループでIncidentレコードをループして出力用の配列に追加します。

(function execute(inputs, outputs) {
  var inc_array = [];
  while (inputs.incidents.next()) {
    var info = {};
    info.number = inputs.incidents.getValue("number");
    inc_array.push(info);
  }
  outputs.incident_info = inc_array;
})(inputs, outputs);

action_2.png
3.Actionの出力は作成した配列になります。
action_3.png

action_4.png

フロー

作成したActionを呼び出すフローを作成します。
1.テスト用ActionなのでPublishをしません。デフォルトではPublishされたいないActionは選択できないので、設定を変更します。右上の3の点をクリックして「Configuration」を選択します。
flow_config_1.png
2.「Show draft actions」を有効にします。これでPublishされていないActionも選択可能になります。
flow_config_2.png
3.次のようなフローを作成します。
flow_1.png
4.テスト用なのでTRIGGERは一回のみ実行するようにします。時間を指定しないといけないのですが、適当な時間を入力しても即時にテスト実行できます。
flow_2.png
5.Incidentターブルからレコードを検出するために「Look Up」Actionを追加します。設定条件に制限はありますが、出来る限りここで条件を絞ります。例えば朝6時に実行するフローで1時から6時までのレコードを取得する場合は「Opened」「on」「Today」にします。それでActionの中で0から1時までの時間のレコオードを対象外としてます。
flow_3.png
6.作成したActionを呼び出します。引数には上のActionで取得したレコードを渡します。
flow_4.png
7.これ以降は取得した変数を表示する処理です。
配列を出力するので「For Each Item in」を利用します。作成したActionの出力を引数にします。
flow_5.png
8.取得した内容(number)を出力します。
flow_6.png

実行結果

1.フローを保存してテストを実行します。
右上の「Test」ボタンを押下します。
test_1.png
2.ダイアログが表示されるので「Run Test」ボタンを押下します。
test_2.png
3.テストが実行されたので結果を見ます。「Flow has been executed. To view the flow, click here.」リンクをクリックします。
test_3.png
4. 「Log」を展開すると結果が表示されます。
test_4.png

test_5.png

以上

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?