function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick ConnerPatrick Conner 

Purge all objects not matching criteria

Hey!

I'd like to purge all objects not matching certain field values.

Is it possible to create a batch class removing all tasks older than a certain date, except the task with an ID matching an Account field "Task ID"? This would leave 1 task with ID = Account.TaskID.

Thanks for any help!
Best Answer chosen by Patrick Conner
Suraj PSuraj P
Create a custom checkbox called Do Not Delete on the Task object in your org. Create a batch class that would query for TaskId__c field on Accounts in the start method, and update the Do Not Delete Tasks with matching TaskId__c to "true" in the execute method. In the finish method of this Batch Class, invoke another Batch class where your query for Tasks with Do Not Delete checkbox unchecked in the start method, and delete them in the execute method. 

All Answers

debradebra
Patrick,
Not 100% clear what you want to do.  But if you can write SOQL to retrieve the tasks to delete then batch apex might work.
Is the field Account.TaskID a custom field on the Account object?     You may want to retrieve all the TaskID values from Accounts and store in a collection object (Sets work well) and then query Tasks based on the date and ID not in the Set.
Debra
Suraj PSuraj P
Create a custom checkbox called Do Not Delete on the Task object in your org. Create a batch class that would query for TaskId__c field on Accounts in the start method, and update the Do Not Delete Tasks with matching TaskId__c to "true" in the execute method. In the finish method of this Batch Class, invoke another Batch class where your query for Tasks with Do Not Delete checkbox unchecked in the start method, and delete them in the execute method. 
This was selected as the best answer
Patrick ConnerPatrick Conner
Thanks Debra and Suraj!