0
0

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 1 year has passed since last update.

[ServiceNow] Client ScriptのonChangeイベントで変更直前の値を取得する

Posted at

はじめに

ServiceNowのClient Scriptでは、onChangeイベントを捉えて、Formの値を取得することができますが、onChange FunctionのoldValue変数は、Formを読み込んだ時の値を保持しているだけで、Submitするまでに複数回、値を変更した場合は、直前の値を取ることはできません。
今回は、Incident FormのChannel Fieldを例に、Client Scriptで、Global変数を定義して、直前の値や変更履歴を取得する方法を取り上げます。

Client Scriptの設定

IncidentのClient Scriptを次のように作成します。

ここでポイントは、Isolate scriptをFalseに設定することです。これによりfunctionネストの外側で、Global変数を定義して、参照することができるようになります。
ただし、この変数は、他のScriptからも参照できてしまうので、名前が衝突しないように、命名を工夫する必要があります。

IncidentChannelOnChange.png

Script
var strIncidentChannelPreviousValue = "";
var arrIncidentChannelHistory = [];

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        strIncidentChannelPreviousValue = oldValue;
        arrIncidentChannelHistory.push(oldValue);
        return;
    }

    //Type appropriate comment here, and begin script below
    alert("Initail value: " + oldValue + "\n" +
        "Previous value: " + strIncidentChannelPreviousValue + "\n" +
        "Currnt value: " + newValue + "\n" +
        "Change history: " + arrIncidentChannelHistory.join(" > ") + " > " + newValue
    );
    strIncidentChannelPreviousValue = newValue;
    arrIncidentChannelHistory.push(newValue);

}

実行結果の確認

IncidentのFormから、Channelの値を変更します。
初期値はEmailでした。それを、適当に複数回変更します。
最初の値がEmailで、Phoneに変更した後、Self-Serviceに変更したことが取得できています。

OnChangeHistory.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?