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

I think, you have to make sure the Extensible field in cmdb_ci table is checked.

Tim61
Kilo Contributor

Hi Karishma,

I run below code in Scripts - Background,it can create a table with two column but it doesn't add Configuration Item in Extends table(refer to attachment file)

How to create a table with Extends table: Configuration Item?

Thanks,
Tim

var gr= new GlideRecord('sys_db_object');
gr.newRecord();
gr.setValue('label','Computer ID1');
gr.setValue('name','u_computer_id1');
gr.setValue('super_class','Configuration Item');
gr.insert();

var gr1= new GlideRecord('sys_dictionary');
gr1.newRecord();
gr1.setValue('name','u_computer_id1');
gr1.setValue('internal_type','String');
                                           
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','String');
                                           
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();

Karishma5
Tera Expert

Hi tim,

Go through the modifications mentioned below in bold.

var gr= new GlideRecord('sys_db_object');
gr.newRecord();
gr.setValue('label','Computer ID1');
gr.setValue('name','u_computer_id1');
gr.setValue('super_class','Configuration Item');

// Since it's a Reference field it will accept sys_id. Hence, Instead of using "Configuration Item" use the hard coded sys_id of the "cmdb_ci " in sys_db_object table. Copy the sys_id of that record and paste it here. 


gr.insert();

var gr1= new GlideRecord('sys_dictionary');
gr1.newRecord();
gr1.setValue('name','u_computer_id1');
gr1.setValue('internal_type','String'); // internal_type field is a reference field which will accept sys_id. Hence insted of using "string" in the query pass the hard coded sys_id. Copy the sys_id of the "string" field record in "sys_glide_object" table and use it here.
                                           
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','String'); // internal_type field is a reference field which will accept sys_id. Hence insted of using "string" in the query pass the hard coded sys_id. Copy the sys_id of the "string" field record in "sys_glide_object" table and use it here.


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();

 

Regards,

Karishma

Tim61
Kilo Contributor

Hi Karishma,

It can show Configuration Item in Extends table after using sys_id of the "cmdb_ci " in sys_db_object table.

Thanks a lot.

Thanks,
Tim

 

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