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?

ServiceNow:onSubmitでMRVSの行数をカウントする

0
Posted at

ServiceNow:onSubmitでMRVSの行数をカウントする

背景

Service PortalのカタログアイテムではMRVS(Multi-Row Variable Set)の行数を直接取得できない。
onSubmit クライアントスクリプトで値を取得・カウントし、別のVariable(数値型)に格納することで後続処理に利用できる。


実装

function onSubmit() {
    // Service Portal上でカタログアイテムのスコープを取得
    var catItem = this.angular.element(this.$('#sc_cat_item').find('sp-variable-layout')[0]).scope();
    var parent_g_form = catItem.getGlideForm();

    var mrvsData = parent_g_form.getValue('mrvs_variable_set_name');

    // MRVSが未入力の場合は1をセット
    if (!mrvsData) {
        parent_g_form.setValue('mrvs_row_count', 1);
        return true;
    }

    // JSON文字列をパースして行数を取得(+1で補正)
    var mrvsRows = JSON.parse(mrvsData);
    var rowCount = mrvsRows.length + 1;

    parent_g_form.setValue('mrvs_row_count', rowCount);
    return true;
}

ポイント

項目 内容
スコープ取得 Service Portalでは g_form が直接使えないため、AngularJSのスコープ経由で getGlideForm() を取得する
MRVSの値 getValue() で取得するとJSON文字列で返ってくるため JSON.parse() が必要
行数の補正 length は0始まりのため +1 して実際の行数に合わせる
未入力時 mrvsData が空の場合は 1 をセットしてスキップ

注意

  • #sc_cat_item および sp-variable-layout はService Portal固有のセレクタ。通常のカタログアイテム(非Portal)では不要
  • MRVSのVariable Set名(mrvs_variable_set_name)と格納先Variable名(mrvs_row_count)は実装に合わせて変更する
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?