Tuesday, January 28, 2014

While creating Dynamic SOQL, which involves Datetime gives ” no viable alternative at character ‘’ ” error. OR value of filter criterion for field ‘CreatedDate’ must be of type dateTime and should not be enclosed in quotes OR How to use Datetime in Dynamic SOQL Query in Salesforce

While creating Dynamic SOQL, which involves Datetime gives ” no viable alternative at character ‘<EOF>’ ” error.
OR
value of filter criterion for field ‘CreatedDate’ must be of type dateTime and should not be enclosed in quotes
OR
How to use Datetime in Dynamic SOQL Query in Salesforce

This error is because of wrong construction of Dynamic Query with Datetime. following code snippet will give idea on how to construct dynamic query for Datetime ?
//format the datetime to make it Dynamic Soql ready
String formatedDt = cutOffDateTime.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
String sql = 'SELECT  a.Id  FROM  Agents_Answer__c a    WHERE  a.Agents_Test_Result__r.Agent_Name__r.IsActive__c = false AND  LastModifiedDate < '+ formatedDt ;

Where, “cutOffDateTime” is variable of datetime type. 

No comments:

Post a Comment