Sript to Remove Records created on a specific date

carlh
Kilo Guru

Can anyone provide a script to remove records created on a specific date?

Something went wrong today and we ended up with Duplicates of all records on our vendor table.

I need a script that will delete all records created on 7/28/2016 in Core_Company table.

Please let me know and thanks!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Carl,



Slight modification to the script shared by Abhniay. Enclosed code in function and added setWorkflow line to turn of notification/BR


Script :


delCoreCompanyRec();


function delCoreCompanyRec()


{


var gr= new GlideRecord("core_company");


gr.addEncodedQuery("sys_created_onON2016-07-28@javascript:gs.dateGenerate('2016-07-28','start')@javascript:gs.dateGenerate('2016-07-28','end')");


gr.setWorkflow(false); //Don't fire Business rule,notifications


gr.deleteMultiple();


}


View solution in original post

21 REPLIES 21

Use this


delContractRec();


function delContractRec()


{


var gr= new GlideRecord("contract");


var contract_query="sys_created_onON2016-08-02@javascript:gs.dateGenerate('2016-08-02','start')@javascript:gs.dateGenerate('2016-08-02','end')";


gr.addEncodedQuery(contract_query);


gr.setWorkflow(false); //Don't fire Business rule,notifications


gr.deleteMultiple();


}


tried it...can't tell if it worked but there are errors


find_real_file.pngfind_real_file.png


Common. your table name is incorrect here. it is ast_contract


So would it look like this?



delast_ContractRec();


function delast_ContractRec()


{


var gr= new GlideRecord("ast_contract");


var contract_query="sys_created_onON2016-08-02@javascript:gs.dateGenerate('2016-08-02','start')@javascript:gs.dateGenerate('2016-08-02','end')";


gr.addEncodedQuery(ast_contract_query);


gr.setWorkflow(false); //Don't fire Business rule,notifications


gr.deleteMultiple();


}


Jim Coyne
Kilo Patron

One or all of these posts might help you in the future:


"Xplore GlideRecord Script" Tool


"Preview in Background Script" Tool


"Preview GlideRecord Script" Tool



They allow you to setup your filter in a List View and then create a script based on that filter for use in some code like a Business Rule, or some other script as well as in the Background Script page or James.Neale excellent Xplore tool - ServiceNow Share - Xplore: Developer Toolkit



They would give you the base script needed to query the records you need and then you could just add the call to "deleteMultiple()" to finish it off.