How to create a table using script

Tim61
Kilo Contributor

Hello,

I want to create table with two column as below setting using script:
Table Label: Computer ID
Table Name: u_computer_id
Extends table: Configuration Item

Column label: Computer NameType: StringMax length: 90Column name: u_computer_name
Column label: Type
Type: StringMax length: 32
Default value: Exports to::Imports from
Display: true
Column name: u_type
How to create above table using script in ServiceNow Scripts - Background?

Thanks,
Tim

1 ACCEPTED SOLUTION

Hi Tim, 

Good to know its working. Please mark it as correct, if you are satisfied with the solution and close this thread.

 

Regards,

Karishma

View solution in original post

14 REPLIES 14

Community Alums
Not applicable

Hi Tim,

 

Could you please explain your scenario? I suggest, creating tables from background script is not a good practice.

Hi  hardit,

Here is my scenario:
I want to create table with two column as below setting using script:
Table Label: Computer ID
Table Name: u_computer_id
Extends table: Configuration Item

Column label: Computer NameType: StringMax length: 90Column name: u_computer_name
Column label: Type
Type: StringMax length: 32
Default value: Exports to::Imports from
Display: true
Column name: u_type
How to create above table using script in ServiceNow Scripts - Background?

Thanks,
Tim

Karishma5
Tera Expert

Hi Tim,

Try below code.

var gr= new GlideRecord('sys_db_object');
gr.newRecord(); 
gr.setValue('label','Computer ID1');
gr.setValue('name','u_computer_id1');
gr.setValue('super_class','353a302af822030059a64c78fda74add'); //sys_id of cmdb_ci
gr.insert();

var gr1= new GlideRecord('sys_dictionary');
gr1.newRecord();
gr1.setValue('name','u_computer_id1');
gr1.setValue('internal_type','747127c1bf3320001875647fcf0739e0'); 
                                            //sys_id of string field type
gr1.setValue('column_label','Computer Name');
gr1.setValue('element','u_computer_name');
gr1.setValue('max_length','90');
gr1.insert();

var gr2= new GlideRecord('sys_dictionary');
gr2.newRecord();
gr2.setValue('name','u_computer_id1');
gr2.setValue('internal_type','747127c1bf3320001875647fcf0739e0'); 
                                           //sys_id of String field type
gr2.setValue('column_label','Type');
gr2.setValue('element','u_type');
gr2.setValue('max_length','32');
gr2.setValue('display','true');
gr2.setValue('default_value','Exports to::Imports from');
gr2.insert();

Hope it helps.

Mark it as helpful or correct if find helpful.

 

Regards,

Karishma

find_real_file.png

www.dxsherpa.com

Tim61
Kilo Contributor

Hi Karishma,

I run above code in Scripts - Background but it show below error:

Operation against file 'sys_db_object' was aborted by Business Rule 'Table Field Validation^a95eaa714f22130004c3f9aba310c74d'. Business Rule Stack:Table Field Validation
Background message, type:error, message: is not currently marked as extensible
#### Compiler Stats ####
Compiles: 800, time: 4,762ms
Total classes: 800, bytecode length: 5,376,385
Total loaders created: 800, unloaded: 0, existing: 800
Interpreted compiles: 46, time: 84ms, total bytes: 60,792
Cache name: "syscache_expression", max: 8,192, size: 846, seeks: 347,461, hits: 345,759, misses: 1,702, flushed: 0, puts: 0
Table is not valid - u_computer_id1
Table is not valid - u_computer_id1

Thanks,
Tim

Hi Tim, 

Run the above code with the sys_id of cmdb_ci in sys_db_object table of your instance. And also check the sys_id of the string field type in sys_glide_object table. And use those sys_ids instead of the sys_ids I provided in the above code.

 

Regards,

Karishma