3
1

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

ServiceNow - JavaオブジェクトとJavaScriptオブジェクト

3
Posted at

概要

ServiceNow本体はJavaで書かれている。そのためにJavaオブジェクトを戻すサーバ側ユーティリティメソッドもある。それらのJavaオブジェクトをJavaScriptで扱おうとするとundefinedになる。

課題

ServiceNowのGlideRecordUtilドキュメントのgetTables()メソッドのスクリプトサンプルを実行するとundefinedになる。

var tables = new GlideRecordUtil().getTables("cmdb_ci_linux_server");
gs.log(tables.join(","));  
// The result would be "cmdb_ci, cmdb_ci_computer, cmdb_ci_server, cmdb_ci_linux_server".

原因

原因はこのメソッドの戻り値の型である。説明に書かれている通りにJava ArrayListを返すので直接.join(",")は出来ない。

Returns a Java ArrayList of the ancestors of the given table name.

対策

Java ArrayListをJavaScriptのArrayListに変換すると問題は解決する。
ServiceNowはJavaオブジェクトをJavaScriptオブジェクトに変換するためにj2jsと提供している。
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_J2jsAPI

次のようにサンプルコードを修正すると正しくテーブル名一覧は表示される。

var tables = new GlideRecordUtil().getTables("cmdb_ci_linux_server");
var tablejs = j2js(tables);
gs.log(tablejs.join(","));

実行結果

*** Script: cmdb_ci_linux_server,cmdb_ci_server,cmdb_ci_computer,cmdb_ci_hardware,cmdb_ci,cmdb

以上

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?