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
Shubhangi JadhavShubhangi Jadhav 

Can anyone help me to write batch class to delete records last modified 7 days

I wrote below code on Product object to test:
I want 
"Days parameter should be configurable"
How can i use label for days

global class TetCleanupBacth_Product implements Database.Batchable<sObject>,Schedulable{
    static Date today1 = Date.today().addDays(-1);

    public Database.QueryLocator start(Database.BatchableContext context)
    {
        return Database.getQueryLocator('Select ID from Tet_LoadProduct__c where LastModifiedDate < :today1' AND status__c in ('Success','Processed'));
    }

    global void execute(Database.BatchableContext context, List<SObject> records)
    {
        delete records;
    }

    global void execute(SchedulableContext ctx) 
    {
        Database.executeBatch(new TetCleanupBacth_Product());
    }
    
     global void finish(Database.BatchableContext BC) {
           
      }
}

But getting error while saving.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Shubhangi,

Can you mention the error you are facing while saving the above class?

Also, I think you can use UI to schedule the above class.

Looking forward for your response.

Regards,
Anutej
Shubhangi JadhavShubhangi Jadhav
Hi Anutej,

Thanks for your response.I am getting error for Query.

Please find attchmemt.
How i can save Days in any label and chnage at any time without touching to code?

Regards,
ShubhangiUser-added imageUser-added image
Shubhangi JadhavShubhangi Jadhav
Hi Anutej, Thank you for your response. I am getting Error For Query.Please find attachment. How can I configure days and change without editing the code? I need to achieve below Point: 1. set up daily jobs to clean up successfully imported records which are older than 7 days. 2. Days parameter should be configurable. 3. Data clean should be done from the following objects. Tet_LoadProduct__c [image: QueryError.png] Regards, Shubhangi
ANUTEJANUTEJ (Salesforce Developers) 
Set<String> statusset= new Set<String>{ 'Success','Processed'};
String query = 'Select ID from Tet_LoadProduct__c where LastModifiedDate < :today1' AND status__c in :statusset' ;

return Database.getQueryLocator(query);

Can you try using the above lines once.