Script to update the operational status field of cmdb_ci_computer table to retired

Arathi B Raj
Mega Contributor

We were trying to run the BG script below to change the operational status of some computers to retired where discovery source is from SCCM and most recent discovery is before Aug 1 2022-

var gr= new GlideRecord("cmdb_ci_computer");
gr.addQuery('discovery_source','SG-SCCM');
gr.addQuery('last_discovered','<','2022-08-01 00:00:00');
gr.addQuery();
while (gr.next()){
    gr.setValue("operational_status",5);
    gr.update();
}

But this was not working.

Can anyone point out what could be wrong here or anyone who can help with a script to achieve this requirement.

9 REPLIES 9

suvro
Mega Sage
Mega Sage

Try this

var gr= new GlideRecord("cmdb_ci_computer");
gr.addQuery('discovery_source','SG-SCCM');
gr.addQuery('last_discovered','<','2022-08-01 00:00:00');
gr.query();
while (gr.next()){
    gr.setValue("operational_status",5);
    gr.update();
}

Vasantharajan N
Giga Sage
Giga Sage
var gr= new GlideRecord("cmdb_ci_computer");
gr.addQuery('discovery_source','SG-SCCM');
gr.addQuery('last_discovered','<','2022-08-01 00:00:00');
gr.query(); // You should use query() to retrieve the eligible records
while (gr.next()){
    gr.setValue("operational_status",5);
    gr.update();
}

Thanks & Regards,
Vasanth

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

1) always form the query by forming it on filter condition and copy from there

2) use updateMultiple() for faster update.

var gr= new GlideRecord("cmdb_ci_computer");
gr.addQuery('discovery_source','SG-SCCM');
gr.addEncodedQuery("last_discovered<javascript:gs.dateGenerate('2022-08-01','00:00:00')");
gr.setValue("operational_status",5);
gr.updateMultiple();

Regards
Ankur

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

@Arathi B Raj 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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