Script to delete records from hardware table

Navneet3
Tera Expert

Hi 

I am looking for a script to delete 5419 records from hardware table, the transform map created assets without serial numbers.

The key is SERIAL # is NULL.

find_real_file.png

 

thank you

 

1 ACCEPTED SOLUTION

Hi,

you didn't see my updated script

you had used incorrect gliderecord object which I corrected 34mins ago

sharing the script again

deleteRecords();

function deleteRecords(){
	try{
		var grAlmHardware = new GlideRecord('alm_hardware');
		grAlmHardware.addEncodedQuery("serial_numberISEMPTY");
		grAlmHardware.setLimit(5);
		grAlmHardware.query();
		gs.info(gralmHardware.getRowCount()); //check count before deleting
		while(grAlmHardware.next())
		{
			grAlmHardware.deleteRecord(); //uncomment once you verify the count got reduced by 5
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

 

find_real_file.png

Regards
Ankur

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

View solution in original post

16 REPLIES 16

Voona Rohila
Kilo Patron
Kilo Patron

Hi Navneet

Try this code

var grCmdbCiHardware = new GlideRecord('cmdb_ci_hardware');
grCmdbCiHardware.addEncodedQuery("serial_numberISEMPTY");
grCmdbCiHardware.deleteMultiple();

Make sure you give proper query.

To validate  the count and delete try this

var grCmdbCiHardware = new GlideRecord('cmdb_ci_hardware');
grCmdbCiHardware.addEncodedQuery("serial_numberISEMPTY");
grCmdbCiHardware.query();
gs.print(grCmdbCiHardware.getRowCount()); //check count before deleting
while(grCmdbCiHardware.next())
{
    //grCmdbCiHardware.deleteRecord(); //uncomment for delet
}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Rolila, Should I run this as background script?

var grCmdbCiHardware = new GlideRecord('cmdb_ci_hardware');
grCmdbCiHardware.addEncodedQuery("serial_numberISEMPTY");
grCmdbCiHardware.query();
gs.print(grCmdbCiHardware.getRowCount()); //check count before deleting
while(grCmdbCiHardware.next())
{
    //grCmdbCiHardware.deleteRecord(); //uncomment for delet
}

thanks

create fix script and wrap the code within function

use try catch block and then run

deleteRecords();

function deleteRecords(){
	try{
		var grCmdbCiHardware = new GlideRecord('cmdb_ci_hardware');
		grCmdbCiHardware.addEncodedQuery("serial_numberISEMPTY");
		grCmdbCiHardware.query();
		gs.print(grCmdbCiHardware.getRowCount()); //check count before deleting
		while(grCmdbCiHardware.next())
		{
			//grCmdbCiHardware.deleteRecord(); //uncomment for delet
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Regards
Ankur

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

@Ankur Bawiskar , Should I run this as background script?