2
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 - テーブル情報の取得

Last updated at Posted at 2020-10-09

ServiceNowの標準テーブルの継承されていない列を取得するスクリプト(ユーザが作成されたテーブルではすべての列が表示されるので下の「ユーザテーブルに追加した列を取得」スクリプトを利用する)

var tableName = 'incident';

var grSys = new GlideRecord('sys_dictionary');
grSys.addEncodedQuery('name=' + tableName + '^internal_type!=collection^internal_typeISNOTEMPTY');
grSys.addOrderBy('column_label');
grSys.query();

while(grSys.next()) {
  gs.info(grSys.column_label + ',' + grSys.element + ',' + grSys.internal_type + ',' + grSys.reference + ',' + grSys.display + ',' + grSys.active + ',' + grSys.sys_created_by + ',' + grSys.sys_created_on);
}

継承されている列も含めたすべてのテーブルの列を取得するスクリプト

var tableName = 'cmdb_ci_linux_server';

var tableUtil = new TableUtils(tableName);
var parentTables = tableUtil.getTables().toArray();

for (var i=0; i<parentTables.length; i++) {
  var table = parentTables[i];
  var grSys = new GlideRecord('sys_dictionary');
  grSys.addEncodedQuery('name=' + table + '^internal_type!=collection^internal_typeISNOTEMPTY');
  grSys.addOrderBy('column_label');
  grSys.query();

  while(grSys.next()) {
    gs.info(table + ',' + grSys.column_label + ',' + grSys.element + ',' + grSys.internal_type + ',' + grSys.reference + ',' + grSys.display + ',' + grSys.active + ',' + grSys.sys_created_by + ',' + grSys.sys_created_on);
  }
}

ユーザテーブルに追加した列を取得

var tableName = 'u_test_extend';

var tableUtil = new TableUtils(tableName);
var parentTables = tableUtil.getTables().toArray();
parentTables.shift();

var parentColumns = [];
for (var i=0; i<parentTables.length; i++) {
  var table = parentTables[i];
  var grSysParent = new GlideRecord('sys_dictionary');
  grSysParent.addEncodedQuery('name=' + table + '^internal_type!=collection^internal_typeISNOTEMPTY');
  grSysParent.addOrderBy('column_label');
  grSysParent.query();

  while(grSysParent.next()) {
    parentColumns.push(grSysParent.element.toString());
  }
}

var grSysCustom = new GlideRecord('sys_dictionary');
grSysCustom.addEncodedQuery('name=' + tableName + '^internal_type!=collection^internal_typeISNOTEMPTY');
grSysCustom.addOrderBy('column_label');
grSysCustom.query();

var customTableColumns = [];
while(grSysCustom.next()) {
  customTableColumns.push(grSysCustom.element.toString());
}

for (var j=0; j<parentColumns.length; j++) {
  var x = customTableColumns.indexOf(parentColumns[j]);
  if (x > -1) {
    customTableColumns.splice(x,1);
   }
}

gs.info(customTableColumns);
2
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
2
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?