+ Start a Discussion

Dynamic Query not working

Hello All,

I was trying to retrieve the records with using the below dynamic soql query in batch class but getting the errors.

Snippet :
Datetime dt = Datetime.now();
      Datetime dt1 = system.now().addMinutes(59).addSeconds(59);

string SOQL = 'Select Id,status,ManagerId__c,Account,Account1__c,Account2__c,Account3__c, Reminder1__c' +
                        ',Reminder2__c FROM Vehicle__C WHERE status = \" +
						String.escapeSingleQuotes('Submitted') + '\' AND Secondary_Status__c = \" + String.escapeSingleQuotes('Pending') + '\" +
						'AND ((Reminder1__c >=' + dt+ 'AND Reminder1__c <= '+ dt1 +')  OR (Reminder2__c >= '+ dt + 'AND Reminder2__c <=' + dt1 +'))';

AnudeepAnudeep (Salesforce Developers) 
Are you able to retrieve records with the same SOQL in an execute anonymous window/Apex Execute in workbench or developer console (Straight SOQL and not dynamic)? 

Also what error are you seeing in your batch class?
Hi Anudeep,
I can able to retrieve the records in the developer console with straight soql query...reg error some where missing single quotes while framing the dynamic query