update records in the fastest way

RC12
Tera Contributor

hi all,

i have a requirement where i have 20 lakh records. and of those 20 lakh records, i have to change to operational status field value to non-production. below is the code that i have written. can any1 suggest me a better way.

var gr=new GlideRecord('cmdb_ci');
    gr.addEncodedQuery('discovery_source=Altiris');
    gr.setLimit(20000);
    gr.query();
    while(gr.next())
        {
            gr.setValue('operational_status',2);
            gr.update();
        }

i will be adding it to a schedule job and will be running it on demand. but if i am using a setlimit of 20k. so when first the job ran, it updated 20k records. when i will be running it again, how will the job know that i have to skip the 20k which i updated recently and do update on the remaining (20lakh-20k records)

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

ensure you add the query for operation status

var gr=new GlideRecord('cmdb_ci');
gr.addEncodedQuery('discovery_source=Altiris');
gr.addEncodedQuery('operational_status!=2');
gr.setLimit(20000);
gr.query();
while(gr.next())
{
    gr.setValue('operational_status',2);
    gr.update();
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using updateMultiple() instead of update()?

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

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

ensure you add the query for operation status

var gr=new GlideRecord('cmdb_ci');
gr.addEncodedQuery('discovery_source=Altiris');
gr.addEncodedQuery('operational_status!=2');
gr.setLimit(20000);
gr.query();
while(gr.next())
{
    gr.setValue('operational_status',2);
    gr.update();
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

to make it faster you can use updateMultiple() and also use setWorkflow(false) in case you wish to block the business rules on table from running on update

ServiceNow Bulk Update/Delete

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

var gr=new GlideRecord('cmdb_ci');
gr.addEncodedQuery('discovery_source=Altiris');
gr.addEncodedQuery('operational_status!=2');
gr.setLimit(20000);
gr.query();
gr.setValue('operational_status',2);
gr.updateMultiple();
gr.setWorkflow('false');

 

is this script correct now? 

and do i need to write gr.query()?