logo

NJP

Sébastien MARTIN on LinkedIn: #servicenow #tablehierarchy | 14 comments

Import · Nov 04, 2022 · article

Did you ever dream to easily extract and visualize the hierarchy of a specific table in ServiceNow ? If yes I can help you ;-) We know, as every ServiceNow fan, there is a UI Link named "Show Schema Map" on the table object to display a very old fashion graphical schema of the table. This feature is available since the big bang _^ The OOB Schema Map is not really usable when the table hierarchy is complex (i.e. "task" or "cmdb_ci" tables). So, What is the best structured object we can build in javascript to create the hierarchy of any table ? JSON Object for sure :-) Below, I create a script to build an JSON object that contains the full hierarchy of a specific table. I select the "task" table as an example. The hierarchy is based on the table field "Extends [super_class]". Then you can copy/past the json object in your Json tool and see the result. Personally, I love the following Json online tool : https://lnkd.in/eM_2ZX4X// SCRIPT TO BUILD THE TABLE HIERARCHY var table_GR = new GlideRecord('sys_db_object');table_GR.get('name', 'task'); // replace 'task' by any table you want var jsonArrayObj = getTableHierarchy(table_GR.getValue('sys_id')); var parentTableObj = { "label": table_GR.getValue('label') + '', "name": table_GR.getValue('name') + '', "sys_id": table_GR.getValue('sys_id') + '', "is_extendable": table_GR.is_extendable + '', "extended_by": [] };parentTableObj.extended_by.push(jsonArrayObj);gs.info(JSON.stringify(parentTableObj)); // Get the table Hierarchy as a JSON Array Object function getTableHierarchy(tableSysId /* Table GlideRecord */ ) { var table = new GlideRecord('sys_db_object'); table.addQuery('super_class', tableSysId); // Extends table table.orderBy('label');table.query(); var tableArray = []; while (table.next()) { var tableObj = {};tableObj.label = table.getValue('label') + '';tableObj.name = table.getValue('name') + '';tableObj.parent = table.super_class.name + ''; tableObj.sys_id = table.sys_id + ''; tableObj.is_extendable = table.is_extendable + ''; if (table.is_extendable == true) { tableObj.extended_by = [];tableObj.extended_by.push(getTableHierarchy(table.getValue('sys_id'))); }tableArray.push(tableObj); } return tableArray; }#ServiceNow #TableHierarchy

View original source

https://www.linkedin.com/posts/s%C3%A9bastien-martin-ba580622_servicenow-tablehierarchy-activity-6994240797491523584-0EnU/?utm_source=share&utm_medium=member_desktop