Narsing1
Mega Sage

Create a script include and copy the below code

Script include Name: TableScript_Utils

Code:

var TableScript_Utils = Class.create();
TableScript_Utils.prototype = {
initialize: function(tableLabel, tableName, extendsto, columnjson) {
this.Label = tableLabel;
this.Name = tableName;
this.Moduleneeded = false;
this.TableExtends = extendsto;
this.ColumnsJSON = columnjson; // comma delimited list of column names. by default, the type of column would be "string". change it as per your requirement.
},
createTable: function(){
var tc = new GlideTableCreator(this.Name, this.Label);
var columnattrs = this.getColumns();
tc.setColumnAttributes(columnattrs);
if(typeof this.TableExtends != 'undefined')
tc.setExtends(this.TableExtends);
tc.update();
gs.sleep(10000); //wait for table to be updated.

},
getColumns: function(){
var attrs = new Packages.java.util.HashMap();
var arrcolumns = this.ColumnsJSON.split(',');
for(var clm = 0; clm < arrcolumns.length; clm++){
var ca = new GlideColumnAttributes(arrcolumns[clm]);
ca.setType("string");
ca.setUsePrefix(true); // this can be changed if you don't want to add "u_" prefix to the column
attrs.put(arrcolumns[clm], ca);
}
return attrs;
},
type: 'TableScript_Utils'
};

 

How to call this

var g = "name,age";
var m = new TableScript_Utils('My script table', 'my_script_table','sys_user', g);
m.createTable();

 

Hope this helps.

Comments
lasse3
Giga Guru

This does not work in Rome.

GlideTableCreator is not allowed in scoped applications
Narsing1
Mega Sage

It works.  Make sure you do these

Script Include changes

While you calling the script, use this code

Example

var g = "name,age";
var m = new global.TableScript_Utils('My script table', 'my_script_table','sys_user', g);
m.createTable();
shiven
Tera Contributor

Works great thanks😃

janani pn
ServiceNow Employee
ServiceNow Employee

Instead of keeping scope as Global, can we create tables within scoped app ?

By doing in this way , there are restrictions imposed in using external APIs

Version history
Last update:
‎01-04-2019 10:56 AM
Updated by: