Fix script for getting the exact count of records updated using "sys_updated_on" field

suvetha3
Kilo Expert

We have a requirement to update the Status field based on the Retired checkbox. If u_retired is true, then install status should change to "Retired" and if it is false, it should be "Installed". We need the exact count of rows updated after executing the fix script.  When we use the below script it's not showing the correct count. Please assist.

var sr= new GlideRecord('cmdb_ci_server');
var count=0;
sr.query();
gs.log("Total Rows:" +sr.getRowCount());
while(sr.next())
{
if(sr.u_retired==false)
{
//Updates the Status to "Installed" if Retired Checkbox is unchecked
sr.install_status = 1;
}
if(sr.u_retired==true)
{
//Updates the Status to "Retired" if Retired Checkbox is checked
sr.install_status = 7;
}
sr.update();
count++;
}
gs.log("Updated Rows:" +count);

If there is any possibilities of getting the exact count using sys_update_on field it would be helpful.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please use 2 counts for retire and install

updated script below

var sr= new GlideRecord('cmdb_ci_server');
var countRetired = 0;
var countInstalled = 0;
sr.query();
gs.log("Total Rows:" +sr.getRowCount());
while(sr.next())
{
if(sr.u_retired==false)
{
//Updates the Status to "Installed" if Retired Checkbox is unchecked
sr.install_status = 1;
countInstalled++;
}
if(sr.u_retired==true)
{
//Updates the Status to "Retired" if Retired Checkbox is checked
sr.install_status = 7;
countRetired++;
}
sr.update();
count++;
}
gs.log("Updated countRetired Rows:" +countRetired);
gs.log("Updated countInstalled Rows:" +countInstalled);

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please use 2 counts for retire and install

updated script below

var sr= new GlideRecord('cmdb_ci_server');
var countRetired = 0;
var countInstalled = 0;
sr.query();
gs.log("Total Rows:" +sr.getRowCount());
while(sr.next())
{
if(sr.u_retired==false)
{
//Updates the Status to "Installed" if Retired Checkbox is unchecked
sr.install_status = 1;
countInstalled++;
}
if(sr.u_retired==true)
{
//Updates the Status to "Retired" if Retired Checkbox is checked
sr.install_status = 7;
countRetired++;
}
sr.update();
count++;
}
gs.log("Updated countRetired Rows:" +countRetired);
gs.log("Updated countInstalled Rows:" +countInstalled);

Regards
Ankur

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

suvetha3
Kilo Expert

Thank you

You are welcome

Regards
Ankur

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