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
@anilbathula@@anilbathula@ 

how to return the query in Batch apex

Hi guys,

 

can u help me in the query.

-----Batch Class---------

 

global class Del_leads implements Database.Batchable<sobject>
{

date d=system.today();
integer i=7;
date z=d-i;
global Database.QueryLocator start(Database.BatchableContext BC)
{

return Database.getQueryLocator('select id,name from lead where createddate<=\'z\' and date_opened__c=null');
}

global void execute(Database.BatchableContext BC,List<Lead> Lds)
{
delete Lds;
}
global void finish(Database.BatchableContext BC)
{
System.debug(LoggingLevel.WARN,'Deleting leads Finished');
}
}

-----------Schedule class-------------------------------

global class bulkdelleads Implements Schedulable{
     global void Execute(SchedulableContext SC){      
        
        Del_leads ld=new Del_leads();
       database.executebatch(ld);     
           
     }
}

 

Thanks

Anil.B

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Try this...

 

global String Query;
date d=system.today();
integer i=7;
date z=d-i;
global Database.QueryLocator start(Database.BatchableContext BC)
{
query= 'select id,name from lead where createddate<= :z and date_opened__c=null';
return Database.getQueryLocator(query);
}