{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1115,"path":"api-reference/server-api-reference/c_TableUtilsAPI.md","title":"TableUtils - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# TableUtils- Global\n\nThe TableUtils script include provides shortcuts for accessing table related information.\n\nThis class is available to server-side scripts.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## TableUtils - TableUtils\\(String tableName\\)\n\nCreates an instance of a TableUtils class.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|The table name|\n\n```\nvar tu = new TableUtils(\"incident\");\n```\n\n## TableUtils - drop\\(String tableName\\)\n\nDrops a database table.\n\n**Note:** Use with extreme caution. Dropping a table permanently deletes the table and all its data. If the table is extended, use dropTableAndExtensions.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Name of the table to drop|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar tu = new TableUtils();\ntu.drop(\"table_that_will_be_lost_forever\");\n```\n\nOutput:\n\n```\ndropping table table_that_will_be_lost_forever\nStarting cache flush\nCache flush complete\nTABLE DROP: admin dropped table table_that_will_be_lost_forever\n```\n\n## TableUtils - dropAndClean\\(String tableName\\)\n\nDrops a database table and cleans up references to the table.\n\n**Note:** Use with extreme caution. Dropping a database table permanently deletes the table and all its data. If the table is extended, use dropTableAndExtensions.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|Name of the table to drop|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar tu = new TableUtils();\ntu.dropAndClean(\"table_that_will_be_lost_forever\");\n```\n\nOutput:\n\n```\ndropping table table_that_will_be_lost_forever\nStarting cache flush\nCache flush complete\nTABLE DROP: admin dropped table table_that_will_be_lost_forever\n*** Script: removing gauges for table_that_will_be_lost_forever\n*** Script: removing forms for table_that_will_be_lost_forever\n*** Script: removing styles for table_that_will_be_lost_forever\n*** Script: removing forms sections for table_that_will_be_lost_forever\n*** Script: removing lists for table_that_will_be_lost_forever\n*** Script: removing related lists for table_that_will_be_lost_forever\n*** Script: removing references to table_that_will_be_lost_forever\n*** Script: removing dictionary entries for table_that_will_be_lost_forever\nBackground message, type:info, message: Table deleted\n```\n\n## TableUtils - dropTableAndExtensions\\(String tableName\\)\n\nDrops a database table, all of it's extended tables, and cleans up references to the tables.\n\n**Note:** Use with extreme caution. Dropping a database table permanently deletes the table and all of its data.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|The table to drop|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar tu = new TableUtils();\ntu.dropTableAndExtensions(\"table_that_will_be_lost_forever\");\n```\n\nOutput:\n\n```\ndropping table parent_table_that_will_be_lost_forever\nStarting cache flush\nCache flush complete\nTABLE DROP: admin dropped table ext_table_that_will_be_lost_forever\nremoving gauges for ext_table_that_will_be_lost_forever\nremoving forms for ext_table_that_will_be_lost_forever\nremoving styles for ext_table_that_will_be_lost_forever\nremoving forms sections for ext_table_that_will_be_lost_forever\nremoving lists for ext_table_that_will_be_lost_forever\nremoving related lists for ext_table_that_will_be_lost_forever\nremoving references to ext_table_that_will_be_lost_forever\nremoving dictionary entries for ext_table_that_will_be_lost_forever\nBackground message, type:info, message: Table deleted\ndropping table parent_table_that_will_be_lost_forever\nStarting cache flush\nCache flush complete\nTABLE DROP: admin dropped table parent_table_that_will_be_lost_forever\nremoving gauges for parent_table_that_will_be_lost_forever\nremoving forms for parent_table_that_will_be_lost_forever\nremoving styles for parent_table_that_will_be_lost_forever\nremoving forms sections for parent_table_that_will_be_lost_forever\nremoving lists for parent_table_that_will_be_lost_forever\nremoving related lists for parent_table_that_will_be_lost_forever\nremoving references to parent_table_that_will_be_lost_forever\nremoving dictionary entries for parent_table_that_will_be_lost_forever\nBackground message, type:info, message: Table deleted\n```\n\n## TableUtils - getAbsoluteBase\\(\\)\n\nReturns the base table name from which the table was extended.\n\n**Note:** For any table under the cmdb\\_ci hierarchy, this method returns cmdb\\_ci and not cmdb, which is the actual base table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|Base table name|\n\n```\nvar table = new TableUtils(\"cmdb_ci_server\");\ngs.print(table.getAbsoluteBase());\n```\n\nOutput: cmdb\\_ci\n\n## TableUtils - getAllExtensions\\(\\)\n\nReturns the list of tables that extend a table, includes the base table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|ArrayList|A list of tables that extend the table, includes the base table.|\n\n```\nvar table = new TableUtils(\"task\");\ngs.print(table.getAllExtensions());\n```\n\nOutput:\n\n```\n[task, incident, issue, kb_submission, sysapproval_group, change_request, change_request_imac, sc_task, \nproblem, sc_req_item, ticket, ast_transfer_order, planned_task, change_task, change_phase, sc_request]\n```\n\n## TableUtils - getHierarchy\\(\\)\n\nReturns a list of all classes participating in the hierarchy of the specified table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|ArrayList|A list of all classes in the specified table's hierarchy.|\n\n```\nvar table = new TableUtils(\"cmdb_ci_server\");\ngs.print(table.getHierarchy());\n```\n\nOutput: \\[cmdb\\_ci\\_server, cmdb\\_ci\\_computer, cmdb\\_ci\\_hardware, cmdb\\_ci, cmdb\\_ci\\_mainframe, cmdb\\_ci\\_linux\\_server, cmdb\\_ci\\_mainframe\\_lpar, cmdb\\_ci\\_esx\\_server, cmdb\\_ci\\_unix\\_server, cmdb\\_ci\\_solaris\\_server, cmdb\\_ci\\_hpux\\_server, cmdb\\_ci\\_aix\\_server, cmdb\\_ci\\_osx\\_server, cmdb\\_ci\\_netware\\_server, cmdb\\_ci\\_win\\_server\\]\n\n## TableUtils - getTables\\(\\)\n\nReturns the table hierarchy.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|ArrayList|A list of table names in the parent hierarchy.|\n\n```\n// Get the hierarchy of tables\nvar table = new TableUtils(\"cmdb_ci_computer\");\nvar tableArrayList = table.getTables();\n\n// Use the j2js method to convert the Java ArrayList to JavaScript\ngs.include(\"j2js\");\nvar tableArray = j2js(tableArrayList);\n\n// Write the value of each element in the JavaScript array\nvar i = 0;\nwhile ( i < tableArray.length ) {\ngs.print(\"Table with index \" + i + \": \" + tableArray[i]);\ni++;\n}\n\n```\n\nOutput:\n\n```\nTable with index 0: cmdb_ci_computer\nTable with index 1: cmdb_ci_hardware\nTable with index 2: cmdb_ci\nTable with index 3: cmdb\n```\n\n## TableUtils - getTableExtensions\\(\\)\n\nReturns a list of tables that extend a table.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|ArrayList|A list of table names that extend the table.|\n\nThis example shows that 58 tables in this instance extend the Computer \\[cmdb\\_ci\\_computer\\] table. \\(Output condensed below.\\)\n\n```\n// Get the tables that extend the table\nvar table = new TableUtils(\"cmdb_ci_computer\");\nvar tableArrayList = table.getTableExtensions();\n \n// Use the j2js method to convert the Java ArrayList to JavaScript\ngs.include(\"j2js\");\nvar tableArray = j2js(tableArrayList);\n \n// Write the value of each element in the JavaScript array\nvar i = 0;\nwhile ( i < tableArray.length ) {\n  gs.print(\"Table with index \" + i + \": \" + tableArray[i]);\n  i++;\n}\n```\n\nOutput:\n\n```\nTable with index 0: cmdb_ci_mainframe_hardware\nTable with index 1: cmdb_ci_handheld_computing\nTable with index 2: cmdb_ci_ucs_blade\nTable with index 3: cmdb_ci_storage_switch\nTable with index 4: cmdb_ci_server\nTable with index 5: cmdb_ci_hmc_server\n…\nTable with index 56: cmdb_ci_pc_hardware\nTable with index 57: cmdb_ci_ucs_rack_unit\n```\n\n## TableUtils - hasExtensions\\(\\)\n\nDetermines if a table has been extended.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the table has extensions.|\n\n```\nvar table = new TableUtils(\"cmdb_ci_server\");\ngs.print(table.hasExtensions());\n```\n\nOutput:\n\n```\ntrue\n```\n\n## TableUtils - isBaseClass\\(\\)\n\nDetermines if a table is a base class, meaning it has no parents and has extensions.\n\nFor example, Task is a base class since it is not extended from another table and has tables extended from it. Sys\\_user is not a base class because it has no parents, but does not have extensions.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_l5p_w1w_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether a table is a base class table meaning it has no parents but has extensions.Valid values:\n\n-   true: Table is a base class.\n-   false: Table is not a base class.\n\n</td></tr></tbody>\n</table>```\nvar table = new TableUtils(\"task\");\ngs.print(\"Task is base class: \" + table.isBaseClass());\n \nvar table = new TableUtils(\"sys_user\");\ngs.print(\"User is base class: \" + table.isBaseClass());\n```\n\nOutput:\n\n```\n Task is base class: true\nUser is base class: false\n```\n\n## TableUtils - isSoloClass\\(\\)\n\nDetermines if the table has no parents and no extensions.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the table has no parent and no table is extended from it.|\n\n```\nvar table = new TableUtils(\"task\");\ngs.print(\"task is solo class: \" + table.isSoloClass());\n \nvar table = new TableUtils(\"cmdb_ci_win_server\");\ngs.print(\"cmdb_ci_win_server is solo class: \" + table.isSoloClass());\n \nvar table = new TableUtils(\"sys_user\");\ngs.print(\"sys_user is solo class: \" + table.isSoloClass());\n```\n\nOutput:\n\n```\ntask is solo class: false\ncmdb_ci_win_server is solo class: false\nsys_user is solo class: true\n```\n\n## TableUtils - tableExists\\(\\)\n\nChecks to see if a table exists.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the table exists.|\n\n```\nvar table = new TableUtils(\"my_table\");\ngs.print(\"Does 'my_table' exist? \" + table.tableExists());\n```\n\nOutput: Does 'my\\_table' exist? false\n\n"}}