Deleting multiple records

Rahul Kathuria
Tera Expert

Hi Everyone,

I am trying to delete around 8k records in our instance. Below script is not working. Do we really require while loop in order for the below script to work properly?

var qry = 'short_description=Warning: could not send message for past 4 hours';

var psc = new GlideRecord('hr');

psc.addEncodedQuery(qry);

psc.setLimit('10');

psc.query();

gs.print("the count is "+psc.getRowCount());

psc.deleteMutiple();

Thanks,

Rahul kathuria

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rahul,



Here is the modified business rule along with comment. Make sure table name is correct


updateHrRecords();


function updateHrRecords()


{


var psc = new GlideRecord('hr'); //Make sure table name is correct


psc.addQuery('short_description=Warning: could not send message for past 4 hours');


psc.setLimit('10');


psc.setWorkflow(false); //Turn off business rule or notifications


gs.print("the count is "+psc.getRowCount());


psc.deleteMultiple();


}


References:


Remove All Data from a Table - ServiceNow Wiki


GlideRecord - ServiceNow Wiki


View solution in original post

9 REPLIES 9

Abhinay Erra
Giga Sage

Here you go



var qry = 'short_description=Warning: could not send message for past 4 hours';


var psc = new GlideRecord('hr');


psc.addEncodedQuery(qry);


psc.setLimit('10');


psc.setWorkflow(false);


gs.print("the count is "+psc.getRowCount());


psc.deleteMutiple();


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rahul,



Here is the modified business rule along with comment. Make sure table name is correct


updateHrRecords();


function updateHrRecords()


{


var psc = new GlideRecord('hr'); //Make sure table name is correct


psc.addQuery('short_description=Warning: could not send message for past 4 hours');


psc.setLimit('10');


psc.setWorkflow(false); //Turn off business rule or notifications


gs.print("the count is "+psc.getRowCount());


psc.deleteMultiple();


}


References:


Remove All Data from a Table - ServiceNow Wiki


GlideRecord - ServiceNow Wiki


A minor correction. Should have been addEncodedQuery() instead of addQuery()


That should work though