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 

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 +'))';

Regards,
VSK98​​​​​​​
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?
VSK98VSK98
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