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
rajsrajs 

Error in Batch Apex Query

Hi,

    I have problem in Batch Apex Query.Below is My Query,

 

 

return DataBase.getQueryLocator('Select Id,Account.Annual_Account_Target_Number__c,Account.Target_for_Q1__c,Account.Target_for_Q2__c,Account.Target_for_Q3__c,Account.Target_for_Q4__c,Territory.Name,Account.Name,Opportunity__c,CloseDate,Projections__c,Closed_Won__c,AccountId,StageName,(Select Id, Name,Start_Date__c,End_Date__c,Opportunity__c,Resource_Amount__c,CreatedDate From OpportunityProducts__r where Start_Date__c >= '+g_StartDate+' AND End_Date__c <= '+ g_EndDate +') from Opportunity where (StageName!=\'Lead\' AND StageName!=\'Closed Lost\') AND (CloseDate >= '+g_StartDate+' AND CloseDate <= '+ g_EndDate +' )ORDER BY CloseDate');

 

 The error message is : System.QueryException: line 1:411 no viable alternative at character ' ' .

 

Please help me to get out from this bug.

 

Thanks! 

 

VarunCVarunC

have you tried using SOQL

return DataBase.getQueryLocator([Select Id,Account.Annual_Account_Target_Number__c,Account.Target_for_Q1__c,Account.Target_for_Q2__c,Account.Target_for_Q3__c,Account.Target_for_Q4__c,Territory.Name,Account.Name,Opportunity__c,CloseDate,Projections__c,Closed_Won__c,AccountId,StageName,(Select Id, Name,Start_Date__c,End_Date__c,Opportunity__c,Resource_Amount__c,CreatedDate From OpportunityProducts__r where Start_Date__c >=: g_StartDate AND End_Date__c <=: g_EndDate) from Opportunity where (StageName!='Lead' AND StageName!='Closed Lost') AND (CloseDate >=: g_StartDate AND CloseDate <=: g_EndDate) ORDER BY CloseDate]);

 

instead of dynamic query? Liek this:

 

 

 

Paul BourdeauxPaul Bourdeaux

I have run into the same issue.  Any ideas?

 

I cannot hardcode the query because it can change based on some criteria.  Anyone even know what this error message means?

edwin_beltranedwin_beltran

I'm almost 100% sure, that is related to the date/datetime fields... how are you passing the dates into the query? as a String? if so, you need parse it to Date/DateTime and pass the variable as: (CloseDate >=:g_StartDate AND'..... and so on... 

 

DateTime myDateTime = DateTime.now();

Opportunity o = Database.Query('Select Id From Opportunity Where CloseDate =: myDateTime');

 

so, instead the way you are doing it: (CloseDate >= '+g_StartDate+' AND.........

 

do it: (CloseDate >=:g_StartDate+ AND...............

 

let me know if helped...

edwin_beltranedwin_beltran

jajajajaja jus realized this is a REALLY OLD post.... jajajajajaja

 

Sorry buddy... :D