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
VSK98VSK98 

System.QueryException: unexpected token: '2020-03-10'

Hello All,

I have written a sample batch to update the field and its working but i'm nt able to cover the code coverage. I'm getting System.QueryException: unexpected token:  '2020-03-10' ...
please find the snippet below.
global class batchAccountUpdateSLA implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
      
      Date d = system.today();
        String query = 'SELECT Id,Name FROM Account where After1day__c =:' + d;
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.SLA__c= 'Gold';            
         }
         update scope;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Test Class:
 
@isTest
 public class batchAccountUpdateSLA_Test{
 Static testmethod void batchAccountUpdateSLA_TestMethod(){
 
 Date d = system.today().addDays(3);
     Account acc = new Account();
     acc.name = 'Test';
     acc.Fax = '12345';
     insert acc;
      Test.StartTest(); 
      batchAccountUpdateSLA bacth = new batchAccountUpdateSLA ();
      ID batchprocessid = database.Executebatch(bacth ); 
      Test.StopTest();
    }
  }

Regards,
VSK98
Best Answer chosen by VSK98
SarvaniSarvani
Hi,

Please try to modify your start method in batch class like below:
 
global Database.QueryLocator start(Database.BatchableContext BC) {
      
       return Database.getQueryLocator([ SELECT Id, name FROM Account WHERE After1day__c = TODAY]);
    }
Hope this helps! Please mark as best if it does

Thanks

All Answers

SarvaniSarvani
Hi,

Please try to modify your start method in batch class like below:
 
global Database.QueryLocator start(Database.BatchableContext BC) {
      
       return Database.getQueryLocator([ SELECT Id, name FROM Account WHERE After1day__c = TODAY]);
    }
Hope this helps! Please mark as best if it does

Thanks
This was selected as the best answer
sachinarorasfsachinarorasf
Hi,

I have gone through your problem.
 
Batch Class: batchAccountUpdateSLA
global class batchAccountUpdateSLA implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Account where After1day__c = TODAY';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.SLA__c= 'Gold';            
         }
         update scope;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Test Class:batchAccountUpdateSLA_Test
@isTest
 public class batchAccountUpdateSLA_Test{
 Static testmethod void batchAccountUpdateSLA_TestMethod(){
 
 Date d = system.today().addDays(3);
     Account acc = new Account();
     acc.name = 'Test';
     acc.Fax = '12345';
     acc.After1day__c = Date.today();
     insert acc;
      Test.StartTest(); 
      batchAccountUpdateSLA bacth = new batchAccountUpdateSLA ();
      ID batchprocessid = database.Executebatch(bacth ); 
      Test.StopTest();
    }
  }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
VSK98VSK98
Hi Sachin Arora,

It will throw exception if we use above query..

Thanks for your support

Regards,
VSK98