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
RajnisfRajnisf 

unexpected token: ')' in batch apex

hi..need a help in query...oh batch class
Error is : unexpected token: ')' 
We would like to move leads currently in the Working or Busy status that do not have completed tasks in the last 30 days to the ABC status.
  • Lead Status = Working, Busy
  • Last Activity Date < Today – 30

We want to scheduale a batch class daily basis...here is the code..
global class Leadinactiveflow implements Database.Batchable<SObject>

{

 global Database.Querylocator start (Database.BatchableContext BC)
  {
         
   String query = 'SELECT Id, Status, LastActivityDate ,(select id, createdDate,status from Tasks )From Lead WHERE Status in (\'Working\', \'Busy\')  AND  (Today() - LastActivityDate) > 30';
   System.debug(query);

      return Database.getQueryLocator(query);          

  }
  global void execute (Database.BatchableContext BC, List <Lead> scope)

  {

   List<Lead> listLead = new List<Lead>();

    for(Lead l : scope)
     {
      
         l.Status = 'ABC';     

      listLead.add(l);

      }

   
         if(!listLead.isEmpty()){
            update listLead;
        } 

  } 

global void finish (Database.BatchableContext BC)

{

}

}
Schedulable class is :
 
global with sharing class SchedulerForBatchApex implements Schedulable

{
    global void execute(SchedulableContext sc)        

  {
      Leadinactiveflow b = new Leadinactiveflow();
      ID BatchID = Database.executeBatch(new Leadinactiveflow(), 200);
  }

Public static void SchedulerMethod() 

    {
    string con_exp='0 0 1 * * ?';

    System.schedule('LeadinactiveflowTest', con_exp, new SchedulerForBatchApex());
                                     
    }   

}


 
Best Answer chosen by Rajnisf
Amit Chaudhary 8Amit Chaudhary 8
You need to update your query as Davy Huijgens suggested

Please check below post Date Formats and Date Literals
1)https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Try below code
global class Leadinactiveflow implements Database.Batchable<SObject>

{

 global Database.Querylocator start (Database.BatchableContext BC)
  {
         
   String query = 'SELECT Id, Status, LastActivityDate ,(select id, createdDate,status from Tasks ) From Lead WHERE Status in (\'Working\', \'Busy\')  AND  LastActivityDate < LAST_N_DAYS:30 ';
   System.debug(query);

      return Database.getQueryLocator(query);          

  }
  global void execute (Database.BatchableContext BC, List <Lead> scope)

  {

   List<Lead> listLead = new List<Lead>();

    for(Lead l : scope)
     {
      
         l.Status = 'ABC';     

      listLead.add(l);

      }

   
         if(!listLead.isEmpty()){
            update listLead;
        } 

  } 

global void finish (Database.BatchableContext BC)

{

}

}

Let us know if this will help you
 

All Answers

Davy HuijgensDavy Huijgens
You can try your query in the developer console. This will work, check if those are the results you want.
 
SELECT Id, Status, LastActivityDate ,(select id, createdDate,status from Tasks )From Lead WHERE (Status = 'Working' OR Status = 'Busy')  AND LastActivityDate < LAST_N_DAYS:30

 
Amit Chaudhary 8Amit Chaudhary 8
You need to update your query as Davy Huijgens suggested

Please check below post Date Formats and Date Literals
1)https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Try below code
global class Leadinactiveflow implements Database.Batchable<SObject>

{

 global Database.Querylocator start (Database.BatchableContext BC)
  {
         
   String query = 'SELECT Id, Status, LastActivityDate ,(select id, createdDate,status from Tasks ) From Lead WHERE Status in (\'Working\', \'Busy\')  AND  LastActivityDate < LAST_N_DAYS:30 ';
   System.debug(query);

      return Database.getQueryLocator(query);          

  }
  global void execute (Database.BatchableContext BC, List <Lead> scope)

  {

   List<Lead> listLead = new List<Lead>();

    for(Lead l : scope)
     {
      
         l.Status = 'ABC';     

      listLead.add(l);

      }

   
         if(!listLead.isEmpty()){
            update listLead;
        } 

  } 

global void finish (Database.BatchableContext BC)

{

}

}

Let us know if this will help you
 
This was selected as the best answer
RajnisfRajnisf
Thanks Davy Huijgens and Amit for ur help!!