Script to update the operational status field of cmdb_ci_computer table to retired
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 04:10 AM
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.
- Labels:
-
Discovery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 04:21 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 04:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 04:49 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2022 01:28 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader