0

I'm getting this error when trying to execute the following. I get my deadline date from a Visualpage which is binded directly to a controller.

QueryException: line 1:322 no viable alternative at character ' '

My Query being executed:

 Select c.CaseNumber,c.Status,c.ID, Account.Name, Account.Status__c, Account.Type,Account.Location_Type__c,Account.BillingCountry,Contact.Name, Contact.Email FROM Case c where c.BatchNumber__c = 'a0dR0000003dfBbIAI' and c.Deadline_Date__c = 2012-11-16 00:00:00

My Code:

  Date deadline = CaseParameter.Deadline_Date__c;
  soql = 'Select c.CaseNumber,c.Status,c.ID, Account.Name, Account.Status__c, Account.Type,Account.Location_Type__c,Account.BillingCountry,Contact.Name, Contact.Email  FROM Case c  where c.BatchNumber__c = \''+batchNumber+'\''; 
  soql+= ' and c.Deadline_Date__c = '+deadline;
raym0nd
  • 3,172
  • 7
  • 36
  • 73

2 Answers2

2

Your date should be 2012-11-16T00:00:00Z . note the T separator, rather than a space between the date & time components, and the addition of a Timezone indicator at the end (GMT in this case)

From apex, you can use the query binding feature, rather than building a soql string, e.g.

Date deadline =CaseParameter.Deadline_Date__c;
List<Case> cases = [select caseNumber,id,Account.Name from Case where deadline__date__c=:deadline];
superfell
  • 18,780
  • 4
  • 59
  • 81
1
datetime deadline =  datetime.newInstance(CaseParameter.Deadline_Date__c,Time.newInstance(0, 0, 0, 0));
  soql = 'Select c.CaseNumber,c.Status,c.ID, Account.Name, Account.Status__c, Account.Type,Account.Location_Type__c,Account.BillingCountry,Contact.Name, Contact.Email  FROM Case c  where c.BatchNumber__c = \''+batchNumber+'\''; 
  soql+= ' and c.Deadline_Date__c = '+deadline.format('yyyy-MM-dd');
Shimshon Korits
  • 1,189
  • 6
  • 10