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 3 years have passed since last update.

ServiceNow - コンマ区切り文字列に含まれているかチェックする

Posted at

概要

ServiceNowフォーラムに挙げられた質問です。
フォームに入力されたコンマ区切りの数字がすべてシステムプロパティで定義されているコンマ区切りの数字に含まれているか確認するスクリプトを作成します。簡単な要件ですが、少しはまりました

問題点

ServiceNowで対応しているJavaScriptのバージョンはECMAScript5です。そのために.some()や.every()が使えない。
https://docs.servicenow.com/bundle/paris-application-development/page/script/JavaScript-engine-upgrade/concept/c_JS_engine_upgrade.html

それではコンマ区切りのも文字列を.split()は配列に変換してループしてindexOf()したらよいはず。ServiceNowのバックグラウンドスクリプトで正常に動作することを確認した後にスクリプトインクルードに複写して動かして見たら、何時もfalseになる。フォームやシステムプロパティからコンマ区切りの数字を取得するのではなく、最初から数字の配列にすると正常に動作する。

問題はスクリプトインクルードの配列にあるようです。バックグラウンドスクリプトでは正常に動作されるけど、スクリプトインクルードでは型が正しく変換されないのか.indexOf()が正しく動作しない。最終的にはsplit()した配列を読み返して数字配列に変換したら正しく動作するようになりました。

スクリプトインクルード

var ArrayValidateUtil = Class.create();
ArrayValidateUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isIncluded: function() {
        var user_prop = this.getParameter('sysparam_check_value').trim();
        var sys_prop = gs.getProperty('ozawa.check.values');

        sys_prop_array = sys_prop.split(',').map(Number);
        user_prop_array = user_prop.split(',').map(Number);

        return sys_prop_array.filter(function(elem) {
            return user_prop_array.indexOf(elem) > -1;
        }).length == user_prop_array.length;
    },

    type: 'ArrayValidateUtil'
});
    type: 'ArrayValidateUtil'
});

クライアントスクリプト

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

    var ajax = new GlideAjax('ArrayValidateUtil');
    ajax.addParam('sysparm_name', 'isIncluded');
    ajax.addParam('sysparam_check_value', newValue);
    ajax.getXMLAnswer(function(answer) {
		if (answer == "true") {
            g_form.showFieldMsg('check_field_name', 'All elements are OK', 'info');
        } else {
			g_form.showFieldMsg('check_field_name', 'Some elements are invalid', 'error');
		}
    });
}
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?