Background script updating only one record at a time;

Meenal Gharat
Giga Guru

Hi All,

I am trying to do Mass update on my Gb Case using background script.

Everytime when I run the script it updates only one record how can i do mass update on 80000 records?

below is the script.

doUpdate();

function doUpdate()
{
var count= new GlideRecord('u_gbs_case_management');
count.addEncodedQuery('state=5^u_resolvedISEMPTY');

count.setLimit(10);
count.query();
if(count.next());
{

count.u_resolved = count.closed_at;
count.update();
}
}

 

any advice would be helpful.

Thanks and regards,

Meenal

1 ACCEPTED SOLUTION

bint
Giga Contributor

Hi Meenal,

 

Use setWorkflow(false) command. it will help you to execute the command smoothly.

please go through the link for more details:

https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_GlideRecord-setWorkFlow_Boolean

 

Run the code in below format:

doUpdate();

function doUpdate()
{
var count= new GlideRecord('u_gbs_case_management');
count.addEncodedQuery('state=5^u_resolvedISEMPTY');

count.setLimit(10);
count.query();
if(count.next());
{

count.setWorkflow(false); 

count.u_resolved = count.closed_at;
count.update();
}
}

 

 

Hope this will resolve your issue. 🙂

View solution in original post

13 REPLIES 13

Hi Mark,

 

After adding gs.print(count.sys_id) it gives me single sys_id on which the record is updated.

Thanks and regards,

Meenal

Can you check in the LIST view how record you are getting 

 

navigate to u_gbs_case_management table in breadcrumbs pass the filter state & resolve and see how records you are getting.

 

Also check the row count in Script too:

 

doUpdate();

function doUpdate()
{
var count= new GlideRecord('u_gbs_case_management');
count.addEncodedQuery('state=5^u_resolvedISEMPTY');

count.setLimit(10);
count.query();

gs.print(count.getRowCount()); // Displays the number of records you are getting after quering the filter
while(count.next());
{

count.u_resolved = count.closed_at;
count.update();
}
}

Sanket Khabiya
Kilo Sage

Hello,

 

var count= new GlideRecord('u_gbs_case_management');
count.addEncodedQuery('state=5^u_resolvedISEMPTY');

count.setLimit(80000);
count.query();
while(count.next());
{

count.u_resolved = count.closed_at;
count.update();
}
}

 

Regards,

Sanket

AbdulAzeez
Mega Guru

Replace If with while which Loops can execute a block of code as long as a specified condition is true.

Paul Curwen
Giga Sage

Mark ^ is correct and obviously you will also need to remove:

 

count.setLimit(10);

 

For such a large number of records not a bad idea to use set limit and run it as a few smaller jobs.

 

Some good practice Background Script tips here: 

 

https://www.servicenowelite.com/blog/2017/8/25/code-vault/background-scripts

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul