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
DJP1SDJP1S 

Annoying DateTime problem with dynamic SOQL...

I keep getting a "no viable alternative at character ' '" error whenever I try to compare a datetime value of an sObject to CreatedDate.

 

Basically, I keep getting this written out in my query:

AND CreatedDate > 2010-01-08 00:00:00

Rather than this:

AND CreatedDate > 2010-01-08T00:00:00Z

This is driving me crazy. Any ideas on how to fix?

 

Here's my visualforce component

<b>Notes Back To:&nbsp;&nbsp;</b>
<apex:inputField value="{!dateFilter.Start_Time__c}"required="false"/>&nbsp;&nbsp;

 Which plugs into this part of my controller

this.query = 'SELECT Name, ClientCode__c, ClientCode__r.Name, DOW_Created__c, Project__c, ID, Account__c, Prompt__c,Note__c, Seq__c, CreatedBy.Name, CreatedDate, Important__c, Note_Type__c, Department__c, Note_Group__r.Name, Page1Task__c, Status__c, Date__c FROM Note__c WHERE CreatedBy.Name != null';
        system.debug('%%% query: ' + this.query); 
        if(this.dateFilter.Start_Time__c != null){
            this.query = this.query + ' AND CreatedDate > ' + dateFilter.Start_Time__c;
            system.debug('%%% query: ' + this.query); 
        }

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

You can try this:

 

DateTime startDate = dateFilter.Start_Time__c;

 

Replace the following line

 

this.query = this.query + ' AND CreatedDate > ' + dateFilter.Start_Time__c;

 

with

 

this.query = this.query + ' AND CreatedDate > :startDate ';  // be sure to declare startDate at the beginning of the function.

All Answers

Kiran  KurellaKiran Kurella

You can try this:

 

DateTime startDate = dateFilter.Start_Time__c;

 

Replace the following line

 

this.query = this.query + ' AND CreatedDate > ' + dateFilter.Start_Time__c;

 

with

 

this.query = this.query + ' AND CreatedDate > :startDate ';  // be sure to declare startDate at the beginning of the function.

This was selected as the best answer
DJP1SDJP1S

Excellent, thank you!