- 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 06:54 AM
runScript();
function runScript(){
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())
{
gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 07:07 AM
If the records being copied are in lakhs just do one thing,
Run fix script=>Process the script in background taking limit of 5k-10k.
& if you want to copy why are you looking up?
runScript();
function runScript(){
var gr = new GlideRecord('u_table_1');
gr.setLimit(5000);
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())
//{
gr1.initialize();
gr1.u_description = gr.u_description;
gr1.u_short_description = gr.u_short_description;
gr1.u_name= gr.u_name;
gr1.insert();
}
//}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 06:58 AM
Hi,
I don't think you can do this faster. 1 Thing tho: You can run the fix script in the background instead. This does not lock your session and also does not get interrupted by the transaction timer.
Regards
Fabian

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2020 06:59 AM
Correction: You can improve runtime. But that will take more time than your current script running in the background.