Need to delete records using background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 04:33 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 04:41 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 04:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 04:44 AM
I want to skip the oldest record from this, let me know how I can do this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 04:49 AM
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.