Background Script to Delete Multiple Incident Records

Scott Shewan
Kilo Contributor

Hi All,

 

Long time reader, first time poster.

 

Due to legal requirements, one of our clients has requested that 5,000+ incident records are deleted. We have been provided with a .csv file that contains all of these incident numbers.

 

We need to create a background script that will delete these records, or failing that, update the resolution notes (or anywhere, really) with something unique so that we can report against this "something unique" and cmd-click through all of them and delete them that way.

 

Truth be told, I'm agnostic as to how we get this done short of deleting them one by one by one by one.

 

Any help would be appreciated. 

 

Thanks,
Scott

1 ACCEPTED SOLUTION

You don't have to add incident numbers in the script, You just need to include those in the Value field of your System property. In the image it was shown in Choices field which is Incorrect and you can clear them off. And then add its name in the script.

find_real_file.png

var incs = gs.getProperty('inc.numbers'); //Your property name here
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN'+incs);
gr.deleteMultiple();

View solution in original post

12 REPLIES 12

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

try this changing the parameter as per your need:

var gr = new GlideRecord('pa_scores');
gr.addQuery('breakdown.name','=', 'Model Category');
gr.addEncodedQuery('sys_created_on<=' + gs.dateGenerate('2016-03-31','23:59:59'));
gr.query();
while (gr.next()) {
   gr.deleteMultiple();
}

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

Nitesh Balusu
Giga Guru

Apart from using Alberto's code, don't use a background script to delete so many records, use a scheduled job instead.

Your transaction will timeout in sometime and all the records will not be deleted, you will probably have to logout log back in and run the script again for probably every 500 records or so. So use a scheduled job instead so that it'll run the background and you can continue working on other things.

 

Alikutty A
Tera Sage

Hello Scott,

Background script will not be suitable for deleting 5000+ records, You will need to write a one time script and execute it in a scheduled job as on-demand.

Since you have numbers in CSV, you can create a system property which will contain a comma separated list of all Incident numbers for eg

INC123,INC223,INC334,INCxxx

Then you could use the following script

var incs = gs.getProperty('inc.numbers'); //Your property name here
var gr = new GlideRecord('incident');
gr.addEncodedQuery('numberIN'+incs);
gr.deleteMultiple();

This should get your records deleted based on incident numbers.

Thanks!

Thanks for getting back to me so quickly. 

When you say "create a system property", what should I put for the Type?

Sorry if this is an obvious question but I'm new to SN so please be gentle!