Copy records from one table to another tables using script.

Peter Wood
Kilo Guru

I have two tables "ABC" and "PQR".Both table contains same fields. In ABC table I have 100 records I want to pull all 100 records in table PQR using script.How to do that?

1 ACCEPTED SOLUTION

i tested with below code in my persona instance. i create custom number field on my custom table and its string type field and it has copied the incident number to the custom number field. 

 

var gr = new GlideRecord('incident');
gr.query();
while(gr.next()){

var gr1 = new GlideRecord('u_custom');
gr1.initialize();
gr1.u_name= gr.short_description;
gr1.u_number = gr.number;
gr1.insert();

 

}

View solution in original post

14 REPLIES 14

Harsh Vardhan
Giga Patron

 

you can use below code here to copy the data from one table to another table. 

 

Sample code:

 

var gr = new GlideRecord('abc');
gr.query();
while(gr.next()){

var gr1 = new GlideRecord('PQR');
gr1.initialize();
gr1.<field name> = gr.<field name>;
gr1.insert();

}


 

Note: add the field name which you want to copy here. 

 

 

Thanks Harshvardhan,

I tried this in background script it is working but I am facing one problem in it.

I have system generated number fields(which generates auto number) but is it not getting copied in custom number field,what is the problem.

 

Hi there,

Do you mean you have on both your ABC and PQR tables auto-number set? And PQR now generates a new number? Or is your question different?

Do you want the old number from ABC, to be used in PQR? Do note: using different Numbers will make the record not findable through the direct search.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

i tested with below code in my persona instance. i create custom number field on my custom table and its string type field and it has copied the incident number to the custom number field. 

 

var gr = new GlideRecord('incident');
gr.query();
while(gr.next()){

var gr1 = new GlideRecord('u_custom');
gr1.initialize();
gr1.u_name= gr.short_description;
gr1.u_number = gr.number;
gr1.insert();

 

}