How to copy all the Fields/columns from one table to another(new Table) through Background scripts-urgent

Vijay27
Tera Guru

please help me

1 ACCEPTED SOLUTION

Hi,

Do you want to copy all records or some of them?

if you want all of them

then write code like 

var gr=new GlideRecord('Live feed message table name');

gr.query();

//gs.print(gr.getRowCount());

while(gr.next())
{
var cs=new GlideRecord('Target table name');
cs.initialize();
//as like bellow you can take values from gr and initialize them 
//make sure that field type shoulb be compatible
   // cs.u_item=gr.sys_id;
   // cs.u_stockroom='ffec3762dba71f004b6cf3561d9619c6';
   // cs.u_stock_number=100;
   // cs.u_stock_limit=10;
cs.insert();
}

 

 

Thanks

View solution in original post

5 REPLIES 5

arielgritti
Mega Sage

Hello Vijay

i'll assume both table has the same structure?
You can query with glideRecord the "source" table and in a while loop insert the rows in the "target" table.

Maybe this is not the more efficient way (depend the number of rows) but works

Ariel

Hi,

Do you want to copy all records or some of them?

if you want all of them

then write code like 

var gr=new GlideRecord('Live feed message table name');

gr.query();

//gs.print(gr.getRowCount());

while(gr.next())
{
var cs=new GlideRecord('Target table name');
cs.initialize();
//as like bellow you can take values from gr and initialize them 
//make sure that field type shoulb be compatible
   // cs.u_item=gr.sys_id;
   // cs.u_stockroom='ffec3762dba71f004b6cf3561d9619c6';
   // cs.u_stock_number=100;
   // cs.u_stock_limit=10;
cs.insert();
}

 

 

Thanks

Something similar to this

//Query the source table
var grSource = new GlideRecord("your_source_table");
grSource.addQuery("field1", "value1"); //to query the source table. If not (all the rows) comment this line
grSource.query();
while (grSource.next()) {
	//Insert into target table
	var grTarget = new GlideRecord("your_target_table");
	grTarget.initialize();
	//Populate the target fields
	grTarget.field1 = grSource.field1;
	grTarget.field2 = grSource.field2;
	grTarget.insert();
}

Please, mark correct, useful or bookmark if I helped you

Thanks

Ariel

What if i dont want to copy all the fields on by one,

So instead of , 

grTarget.field1 = grSource.field1;
	grTarget.field2 = grSource.field2;

can i run a loop or something to copy all fields?

 

 

-Neil