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
My OwnMy Own 

Reg: SOQL Error in Dynamic SOQL with DateTime

 

 

Hi All,  

   I am trying to get the list of documents based on createdDate using Dynamic SOQL.

   I am getting SOQL Exception in this.  Please see the sample code. Any help would be greatly appreciated.    

 

Sample Code:  

Datetime fromDate = datetime.newInstance(2005,10,08, 1,02,03); 

try{ 

string sUserQuery = 'Select Name,CreatedDate From Document where CreatedDate >' + fromDate; 

system.debug('**User Query**' + sUserQuery); 

List<Sobject> lstQueryRecords = Database.query(sUserQuery); 

system.debug('--------lstQueryRecords-------' + lstQueryRecords); 

}

 catch(Exception ex){

system.debug(ex.getMessage()); 

 

Debug Logs :  System.QueryException: line 1:76 no viable alternative at character '<EOF>'

                          DEBUG|line 1:76 no viable alternative at character '<EOF>'

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'll need to use the Apex variable syntax rather than just concatenating the datetime:

 

string sUserQuery = 'Select Name,CreatedDate From Document where CreatedDate > :fromDate'; 

 

All Answers

bob_buzzardbob_buzzard

You'll need to use the Apex variable syntax rather than just concatenating the datetime:

 

string sUserQuery = 'Select Name,CreatedDate From Document where CreatedDate > :fromDate'; 

 

This was selected as the best answer
ANKITAANKITA

It also gives the same result:

 

Datetime fromDate = datetime.newInstance(2005,10,08, 1,02,03);
system.debug('**** From Date ****'+fromDate); // *2005-10-07 19:32:03
try{
List<Sobject> sUserQueryRecords=[Select Name,CreatedDate From Document where CreatedDate >: fromDate];
system.debug('--------lstQueryRecords-------' + sUserQueryRecords);
}
 catch(Exception ex){
system.debug(ex.getMessage());
} 

 

Thanks

Ankita