Creating multiple columns in table

Harshit18
Giga Contributor

Hi 

In order to create a new target table, I have to first create table (50) columns in ServiceNow manually. 
I want to know if I can create columns automatically in target table. My excel sheet data has nothing to do with task table columns or any other OOB table columns. So, inheriting the parent table is not the solution.
Please suggest me if there is any automation or configuration available for creation of multiple columns in ServiceNow. 

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

What are the types of all columns that you need to create?

vat tables = [
{ "name" : "table1", fields : [
{"name" : "field1", "type": "string"},
{"name" : "field1", "type": "string"},


]},


];

Then you can run a script in the background to create the tables:

for (var i = 0; i < tables.length; i++){

var table = new GlideRecord('sys_db_object');
table.initialize();
table.name = tables[i].name;
tableSysID = table.insert();
var fields = tables[i].fields;
for (var j = 0; j < fields.length; j++){

var field = new GlideRecord('sys_dictionary');
field.initialize();
field.name = tableSysID;
field.column_label = fields[j].name;
field.internal_type = fields[j].type;
field.insert();

}

}

 

Thanks,

Abhishek

Thank you,
Abhishek Gardade

View solution in original post

3 REPLIES 3

AbhishekGardade
Giga Sage

What are the types of all columns that you need to create?

vat tables = [
{ "name" : "table1", fields : [
{"name" : "field1", "type": "string"},
{"name" : "field1", "type": "string"},


]},


];

Then you can run a script in the background to create the tables:

for (var i = 0; i < tables.length; i++){

var table = new GlideRecord('sys_db_object');
table.initialize();
table.name = tables[i].name;
tableSysID = table.insert();
var fields = tables[i].fields;
for (var j = 0; j < fields.length; j++){

var field = new GlideRecord('sys_dictionary');
field.initialize();
field.name = tableSysID;
field.column_label = fields[j].name;
field.internal_type = fields[j].type;
field.insert();

}

}

 

Thanks,

Abhishek

Thank you,
Abhishek Gardade

Harshit18
Giga Contributor

Cool Abhishek! This is what I was looking for. Can you please help me with providing a link to play with arrays in ServiceNow ?

kjmauriello
Mega Expert

I need to do something similar, however, the table is already created.

The fields I need to create are already on another table (scoped) and I want to replicate them in the new table, so I would like to read the current fields, build out the field JSON to replicate the fields and add them to the table.  Again, the table already exists.

What are the possible items for the field JSON.  You show name and type, how about ref qualifier, reference and all the other items that I can use to create the field(s).

 

Thanks in advance.