Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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.setWorkflow('false');
gr.updateMultiple();

 

is this script correct now?

Hi,

looks fine

Regards
Ankur

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