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
Craig PhoenixCraig Phoenix 

Apex Batch Query Records By Last Modified

Hi,

I am building and working on an Apex Batch Class and almost got it built but for some reason I am not able to resove an error on line 4 and I am hoping someone can help me understand the error and help me with adjusting as needed?
 
global class LeadDailyUpdater implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext bc){
    	return Database.getQueryLocator [select id, LastModifiedDate, Status, leadsource from Lead Where LastModifiedDate < LAST_N_DAYS:30];
    }
    
    global void execute(Database.BatchableContext bc,List<lead> scope)
    {
        List<Lead> leads=new List<Lead>();
        for(lead l : scope)
        {
            l.LeadSource='Closed';
            leads.add(l);
        }
        update leads;
    }
    
    global void finish(Database.BatchableContext bc)
    {
      AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id = :bc.getJobId()];
        system.debug(job);
    }     
}

The only item that is throwing an error is line 4 which I highlighted in code above (line where starts return Data base.getQueryLocator...

Effectively I am looking to pull all leads with last modified date greater than 30 days then update the status to Closed.
Best Answer chosen by Craig Phoenix
SwethaSwetha (Salesforce Developers) 
HI Craig,
It seems to be a syntax error. Try below
global Database.QueryLocator start(Database.BatchableContext bc){
    	return Database.getQueryLocator ([select id, LastModifiedDate, Status, leadsource from Lead Where LastModifiedDate < LAST_N_DAYS:30]);
    }
Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_batch.htm

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
 

All Answers

SwethaSwetha (Salesforce Developers) 
HI Craig,
It seems to be a syntax error. Try below
global Database.QueryLocator start(Database.BatchableContext bc){
    	return Database.getQueryLocator ([select id, LastModifiedDate, Status, leadsource from Lead Where LastModifiedDate < LAST_N_DAYS:30]);
    }
Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_batch.htm

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
 
This was selected as the best answer
Craig PhoenixCraig Phoenix
Thanks a million Swetha! That did it... I had totally forgot that when doing the SOQL queries with the QueryLocator in batch classes that you need a () around the usual [] - Thanks so much!
SwethaSwetha (Salesforce Developers) 
Happy that I could help. Cheers! :)