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
Dave ShulmanDave Shulman 

System.QueryException: no viable alternative at character ' '

Have no idea why im getting this error. From what i found on the community people were saying that i could have a weird quote or something, but I replaced all the single quotes and still getting this error.  Does any one know what this might be.

System.QueryException: line 1:94 no viable alternative at character ' '
 
global class DeleteHalfYearSales implements Database.Batchable <sObject>, Database.Stateful, Schedulable{
    private static final Date eightMonthsAgoBatch = Date.newInstance(Date.today().addMonths(-7).year(), Date.today().addMonths(-7).month(), 1);
    global void execute(SchedulableContext sc){
        DeleteHalfYearSales batch = new DeleteHalfYearSales();
        Database.executeBatch(batch);
    }
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT ID FROM Sales_Data__c WHERE Sales_Type__c = \'Actual\' AND Date__c >= ' + eightMonthsAgoBatch + ' ORDER BY Date__c DESC';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Sales_data__c> scope){
        delete scope;    
    }
    global void finish(Database.BatchableContext BC){
		AsyncApexJob a = [SELECT ID, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems FROM AsyncApexJob WHERE ID = :bc.getJobId()];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> ToAddress = new List<String>();
        ToAddress.add('david.shulman@pdihc.com');
        mail.setToAddresses(ToAddress);
        mail.setSubject( 'The status of execute batch DeleteHalfYearSales');
        mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
            ' batches with ' + a.NumberOfErrors + ' failures.');
        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        }
        catch(exception e){
            System.debug('>>>error: ' + e.getMessage());
        }
    }
}
Test Class
@isTest
public class DeleteHalfYearSalesTest {
	@isTest
    public static void testSetup() {
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test',
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      Cmpid__c = 'CMP234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        insert SDList;
        ID Var = SDList[0].ID;
                
        Test.startTest();
        DeleteHalfYearSales d = new DeleteHalfYearSales();
        Database.executeBatch(d);
        Test.stopTest();
        
        Sales_Data__c Sales = [SELECT ID FROM Sales_Data__c WHERE ID = :Var Limit 1];
        System.assertEquals(null, Sales.Id);
        System.debug('Expected null Actual ' + Sales.Id);
    }    
}


 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Dave,

As mentioned in the below link: https://salesforce.stackexchange.com/questions/3865/no-viable-alternative-at-character-what-does-this-mean

Can you try checking the best answer in case if this comes in handy can you try marking this as the best answer so that it can be useful to others in the future.

Regards,
Anutej