Apex Job to delete records
I think, more than once you had a task of regular cleaning up obsolete or unnecessary records. I advise you about a simple solution that allows you to schedule this process. Please note that this solution is only available for Enterprise, Unlimited, and Developer editions.
public with sharing class DeleteRecordsJob implements Database.Batchable<sObject>, Schedulable { private final String query; public DeleteRecordsJob(String query) { this.query = query; } // Schedulable method public void execute(SchedulableContext ctx) { Database.executeBatch(new DeleteRecordsJob(query)); } // Database.Batchable methods public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator(query); } public void execute(Database.BatchableContext bc, List<sObject> scope) { delete scope; } public void finish(Database.BatchableContext bc) { } }
Here is an example of a batch job that deletes accounts that does not have any activities for the last five years.
DeleteRecordsJob j = new DeleteRecordsJob('SELECT ID FROM Account WHERE LastActivityDate < LAST_N_YEARS:5'); Database.executeBatch(j);
It's also easy to schedule the job. Here is an example of a schedule job, which deletes all the converted leads every Saturday.
DeleteRecordsJob j = new DeleteRecordsJob('SELECT ID FROM Lead WHERE IsConverted = TRUE'); System.schedule('Delete Converted Leads', '0 0 0 ? * SAT *', j);
This class is ready for use in production code, but before this, I recommend you to make some modifications:
- Add another parameter to the constructor - batchSize, which is necessary in order to control batch size in the schedule jobs. Sometimes there is a lot of logic on triggers, because of what at a size of 200 (default value) the limits can fall(for example SOQL, DML or CPU limits).
- Use Database Class methods instead of DML Statements with option allOrNone = false. There may be a situation when only a few records will throw exceptions. In DML statement "delete" option allOrNone is always true. That means that if there will be at least one "invalid" in a stack of records, then the whole pack will be rolled back.
- In the method "finish" add sending the email, if an error occurred during the running of the job. A list of failed records and a corresponding list of errors will simplify the life of administrators and developers.
- As the SOQL query is specified by a string, then it's possible when it turns out that query invalid only when a job starts. Therefore, I suggest you add a SOQL query validation to the constructor. To do this just add "LIMIT 1" (do not forget the handle situation when the query already contains "LIMIT") and call Database.query method. If the query is invalid, then an exception will be thrown.
Useful links:
- Apex Developer Guide » Apex Scheduler
- Apex Developer Guide » Using Batch Apex
- Trailhead » Schedule Jobs Using the Apex Scheduler
- Trailhead » Use Batch Apex
- Online tool » Cron Maker - this service can generate invalid cron. For example, "0 0/1 * 1/1 *? *" (every minute) in Salesforce world is invalid, since Salesforce does not allow to run the Job every minute. Therefore, before use, I advise you to read the salesforce documentation.
- Apex Developer Guide » Bulk DML Exception Handling
- SOQL and SOSL Reference » Date Formats and Date Literals
Comments powered by CComment