Need to delete records using background script

SNow35
Giga Guru

Hi All,

I have a table where the user has created records by mistake now I want to delete those records which are created in that specific time period but want to keep the oldest record. 

Let me know hoe we can achieve this.

4 REPLIES 4

Ragini Kukade
Kilo Guru

Hi,

hope the following link will help you:

https://community.servicenow.com/community?id=community_question&sys_id=658a8be9db5cdbc01dcaf3231f961939

Mark it as correct/ helpful,if it helps you.

Regards,

Ragini

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

use the condition builder to create the query as per your need, then put the query into the following script, modify the name of table and run it using the background script or an on demand scheduled job:

var Table = "<your table>";
var DelQuery = "<your query>";
var DelObj = new GlideRecord(Table);
DelObj.addEncodedQuery(DelQuery);
DelObj.query();
gs.print("DeleteRowCount: "+DelObj.getRowCount());
DelObj.deleteMultiple(); // uncomment this to perform delete operation

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

I want to skip the oldest record from this, let me know how I can do this

SNow35
Giga Guru

The requirement here is, the record numbers are same but they differ with other fileds. Now I have to delete those records with same values in those 4 fields and if I have 10 records then I need to delete all the latest 9 records and leave the oldest one.