How to copy a record from one table to another table

Carol543
Mega Contributor

I want to copy all the records in table A to table B.
Is there a way to copy in a script without using record export / import?

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Carol543 ,

You can write a background script

 

var gr = new GlideRecord('tableA');
gr.query();

gr.setLimit(1); // test for 1 record after that use 100 or 500 as limit so that browser will not hang
while (gr.next()){
var rec = new GlideRecord("tableB");

rec.initialize();

rec.field = gr.field; // map the field name like this

rec.insert(); // insert to b table
}

 

Regards,

Gunjan Kiratkar 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

8 REPLIES 8

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Carol543 ,

You can write a background script

 

var gr = new GlideRecord('tableA');
gr.query();

gr.setLimit(1); // test for 1 record after that use 100 or 500 as limit so that browser will not hang
while (gr.next()){
var rec = new GlideRecord("tableB");

rec.initialize();

rec.field = gr.field; // map the field name like this

rec.insert(); // insert to b table
}

 

Regards,

Gunjan Kiratkar 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Let me try this script.

Hi i have another requirement which is i created 5 tables with columns as A and B . In first table B column have value like 5 and it is a integer data type. Remaining 4 tables also have A and B columns and B columns have same value 5 Now i want to add all 5 table B column values like 5+5+5+5+5 and output will come as 25. I want to paste ouput in another new table which have Column name as output. Could you please suggest me the how can we write business rule for this scenario

 

Mike_R
Kilo Patron
Kilo Patron

Is this a one time copy/data migration?

You can create a fix script or create a flow in flow designer (with a "run once" trigger)