- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 06:52 AM
Hello,
I have a fix script that copies records from one table and inserts to another table. There are over 500,000 records being copied over. The fix script works fine, however, when I run the query it takes a very long time to run and will often timeout. In order for me to fully execute the fix script, I must run it several times. This is just a one off so we want to do this using a fix script. Below is the script I have written.
var gr = new GlideRecord('u_table_1');
gr.query();
while (gr.next()){
var gr1 = new GlideRecord('u_table_2');
gr1.addQuery('u_description',gr.u_description);
gr1.addQuery('u_short_description',gr.u_short_description);
gr1.addQuery('u_name',gr.u_name);
gr1.query();
if(gr1.next())
{
//do nothing
}
//if not found then insert
else{
gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
}
}
Is there maybe a way to segment the fix script so that it runs and completes much quicker?
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 11:18 AM
Try this.
var gr = new GlideRecord('u_table_1');
gr.query();
while (gr.next()){
var gr1 = new GlideRecord('u_table_2');
gr1.addQuery('u_description',gr.u_description);
gr1.addQuery('u_short_description',gr.u_short_description);
gr1.addQuery('u_name',gr.u_name);
gr1.setLimit(1);
gr1.query();
if(!gr1.hasNext()){
gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.setWorkflow(false); //stop execution of Business rule or other scripts.
gr1.insert();
}
}
If it stops in 5 mins then try running in background script then check logs if it throws any other issues. Also, can you confirm - both the tables are in same scope?
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 08:29 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 07:01 AM
Hi,
500,000 records copying will take time and it is not recommended that you do this at once. I suggest that you do this in chunks, may be 1000 per run and run this in scheduled job to run every 5 mins.
Also, create a field in your source table copied and whenever you ocpy that record, update thtat to true. Find below the code.
var gr = new GlideRecord('u_table_1');
gr.addQuery("copied","!=","true");
gr.setLimit(1000);
gr.query();
var gr1 = new GlideRecord('u_table_2');
while (gr.next()){
gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
gr.copied="true";
gr.update();
}
Mark the comment as a correct answer and also helpful if it helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 11:38 PM
Hi,
For a simple and efficient solution, kindly use the approach that i have suggested. This way you don't need to worry how many are copied and how many are left out as we are tracking them using a separate column. You can delete the column once the requirement is done.
Mark the comment as a correct answer and helpful if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 07:14 AM
Hi,
You can run a script when you know you have 500,000 records. I would suggest you to include setLimit(20000); which updates 20K records.
Other simplest way is after appliying the query to the list export it to Excel and import to your target table u_table_1. Simple as easy to do 🙂
Let me know if you need help on Import sets
Please mark it as helpful or correct based on the impact
Abdul Azeez
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 07:41 AM