Script to fetch all fields from a table which is not inherited

ShAn21
Tera Guru

HI All,

I have a table which is extended from CMDB CI. There are also many custom fields created in the child table.

Does anyone have a way to fetch the list all the custom fields from the child table via script?. it should only give results of non extended fields belonging to the child. It a huge table so I cannot check fields one by one.

I have done this before but somehow unable to get hold of the script again.

 

Thanks

Shreyas

1 ACCEPTED SOLUTION

My bad. The script worked on extended tables in cmdb but seems like it doesn't for custom tables.

Fixed the script.

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);

View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Very useful script :). Thank you Hitoshi