Can I create a custom table via a script?

Todd O
Tera Guru

Is it possible to create a SN custom table via a script? I would like to create many tables that are coming from a different database. I would like to write a script to read in that ddl and then translate that to whatever SN needs to create these tables. It will save me a lot of typing. Thank you in advance.

Todd

1 ACCEPTED SOLUTION

Here's an example of adding a new extended table and a column



// create a new table 'abc' by extending 'task' and add new string column 'myfield'


var table_name = 'abc', extends_table = 'task', fname = 'myfield';



var attrs = new Packages.java.util.HashMap();
var ca = new GlideColumnAttributes(fname);
ca.setType("string");
ca.setUsePrefix(false);
attrs.put(fname, ca);



var tc = new GlideTableCreator(table_name , table_name);
tc.setColumnAttributes(attrs);
if(typeof extends_table != 'undefined') tc.setExtends(extends_table);
tc.update();



You can run this in Background Script and go to System Definition > Tables or Tables & Columns to see the new table and column.


View solution in original post

20 REPLIES 20

After create the table, did you change the glide.app.creator.global property to false. In other words, After creating the table, did you change the table scope to application scope instead of global scope?



Regards,


Rajesh


I am able to finish this under application scoped script and now can create import set/staging tables from the script.



--Raj


Hi John,



is it possible to set to TRUE the flag "Extensible" of a table?


i'm trying to create a background script to create multiple extended table at time.



thanks!


Hi Mattia,



Yes, it's possible to set the "Extensible" flag to true for a table:



var gr = new GlideRecord('sys_db_object');


gr.query('name','mytable');


if (gr.next()) {


  gs.info(gr.is_extendable);


  gr.is_extendable = true;


  gr.update();


}


Great! Thanks John!


where I can find all these information about the method and variable for GlideRecord? tipically I search in API reference on Developer site but "is_extendable" is not visible...



thank again