Create a Table via Script
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.
Labels:
https://www.servicenow.com/community/developer-articles/create-a-table-via-script/ta-p/2314145
