- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 01:51 AM - edited 02-16-2025 11:43 PM
Hi all,
I have wrote below code for scheduled job to delete all the records for specific condition.
But i want to delete all the records in a batch of 5000,since there will be excess data.
How can i do that?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 01:54 AM
try this
var tableArray = ["x_tsigh_ci_handler_technical_ci_queue", "x_tsigh_ci_handler_ci_transfer_protocol", "x_tsigh_ci_handler_ci_transfer_status", "x_tsigh_ci_handler_ci_chunks"];
gs.info("@PD Line 3");
for (var i = 0; i < tableArray.length; i++) {
gs.info("@PD Line 5 " + tableArray[i]);
var techCI = new GlideRecord(tableArray[i]);
techCI.addEncodedQuery("sys_created_on>javascript:gs.beginningOfOneYearAgo()");
var batchSize = 5000;
var deletedCount = 0;
do {
techCI.setLimit(batchSize);
techCI.query();
var count = techCI.getRowCount();
gs.info("@PD line 13 row count: " + count + " Table array: " + tableArray[i]);
while (techCI.next()) {
techCI.deleteRecord();
deletedCount++;
}
gs.info("@PD line 19 " + techCI.getEncodedQuery());
} while (count == batchSize);
gs.info("Total records deleted from " + tableArray[i] + ": " + deletedCount);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 01:54 AM
try this
var tableArray = ["x_tsigh_ci_handler_technical_ci_queue", "x_tsigh_ci_handler_ci_transfer_protocol", "x_tsigh_ci_handler_ci_transfer_status", "x_tsigh_ci_handler_ci_chunks"];
gs.info("@PD Line 3");
for (var i = 0; i < tableArray.length; i++) {
gs.info("@PD Line 5 " + tableArray[i]);
var techCI = new GlideRecord(tableArray[i]);
techCI.addEncodedQuery("sys_created_on>javascript:gs.beginningOfOneYearAgo()");
var batchSize = 5000;
var deletedCount = 0;
do {
techCI.setLimit(batchSize);
techCI.query();
var count = techCI.getRowCount();
gs.info("@PD line 13 row count: " + count + " Table array: " + tableArray[i]);
while (techCI.next()) {
techCI.deleteRecord();
deletedCount++;
}
gs.info("@PD line 19 " + techCI.getEncodedQuery());
} while (count == batchSize);
gs.info("Total records deleted from " + tableArray[i] + ": " + deletedCount);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 03:33 AM
I am getting row count 0.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 03:59 AM
are you sure your encoded query is working fine?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 04:24 AM
yes. There are records. still getrowcounts is 0.