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
anurajanuraj 

Error

Hi

While searching for date I am getting this error 

 

no viable alternative at character ' '

 

 

the code is 

if (StartDate != null)
      soql += ' and Start_Date__c equals ' +  myDate ;

 please help me 

thanks 

anuraj

sharrissharris

Does this work?

 

if (StartDate != null)
      soql += ' and Start_Date__c = \'' + myDate + '\'';

 

spraetzspraetz

Are you getting the error when performing the query or when compiling the code?

 

If you're getting it when performing the query, could you please do a Sytemd.debug of the query string and post that?

anurajanuraj

No it is not working.

anurajanuraj

Hi spraetz

the debug log is 

21:26:33.094 (94438000)|USER_DEBUG|[86]|DEBUG|++++++++++++++++++++++++++++++++++++++++++++++++++++++++StartDate 2011-09-15
21:26:33.094 (94474000)|SYSTEM_METHOD_EXIT|[86]|System.debug(ANY)
21:26:33.094 (94517000)|SYSTEM_METHOD_ENTRY|[88]|MAP.get(ANY)
21:26:33.094 (94553000)|SYSTEM_METHOD_ENTRY|[88]|System.PageReference.getParameters()
21:26:33.094 (94585000)|SYSTEM_METHOD_ENTRY|[88]|ApexPages.currentPage()
21:26:33.094 (94621000)|SYSTEM_METHOD_EXIT|[88]|ApexPages.currentPage()
21:26:33.094 (94658000)|SYSTEM_METHOD_EXIT|[88]|System.PageReference.getParameters()
21:26:33.094 (94700000)|SYSTEM_METHOD_EXIT|[88]|MAP.get(ANY)
21:26:33.094 (94760000)|SYSTEM_METHOD_ENTRY|[92]|String.equals(String)
21:26:33.094 (94796000)|SYSTEM_METHOD_EXIT|[92]|String.equals(String)
21:26:33.094 (94836000)|SYSTEM_METHOD_ENTRY|[94]|System.debug(ANY)
21:26:33.094 (94876000)|USER_DEBUG|[94]|DEBUG|++++++++++++++++++++++++++++++++++++++++++++++++++++++++AppoinmentType 
21:26:33.094 (94912000)|SYSTEM_METHOD_EXIT|[94]|System.debug(ANY)
21:26:33.094 (94951000)|SYSTEM_METHOD_ENTRY|[95]|String.equals(String)
21:26:33.094 (94986000)|SYSTEM_METHOD_EXIT|[95]|String.equals(String)
21:26:33.095 (95027000)|SYSTEM_METHOD_ENTRY|[97]|System.debug(ANY)
21:26:33.095 (95078000)|USER_DEBUG|[97]|DEBUG|++++++++++++++++++++++++++++++++++++++++++++++++++++++++Clint 
21:26:33.095 (95117000)|SYSTEM_METHOD_EXIT|[97]|System.debug(ANY)
21:26:33.095 (95334000)|SYSTEM_METHOD_ENTRY|[103]|System.debug(ANY)
21:26:33.095 (95392000)|USER_DEBUG|[103]|DEBUG|++++++++++++++++++++++++++++++++++++++++++++++++++++++++myDate 2011-09-15 00:00:00
21:26:33.095 (95432000)|SYSTEM_METHOD_EXIT|[103]|System.debug(ANY)
21:26:33.095 (95470000)|SYSTEM_METHOD_ENTRY|[104]|String.equals(String)
21:26:33.095 (95507000)|SYSTEM_METHOD_EXIT|[104]|String.equals(String)
21:26:33.095 (95548000)|SYSTEM_METHOD_ENTRY|[106]|System.debug(ANY)
21:26:33.095 (95588000)|USER_DEBUG|[106]|DEBUG|++++++++++++++++++++++++++++++++++++++++++++++++++++++++Status
21:26:33.095 (95622000)|SYSTEM_METHOD_EXIT|[106]|System.debug(ANY)
21:26:33.095 (95692000)|METHOD_ENTRY|[108]|01p900000008Gld|apposearchController.runQuery()
21:26:33.096 (96059000)|SYSTEM_METHOD_ENTRY|[58]|System.debug(ANY)
21:26:33.096 (96116000)|USER_DEBUG|[58]|DEBUG|select Appointment_Type__c, Client__c, Start_Date__c, Status__c, Client_First_Name__c from Appointment1__c where Client_First_Name__c != null and Start_Date__c  =2011-09-15 00:00:00 order by Appointment_Type__c asc limit 20
21:26:33.096 (96167000)|SYSTEM_METHOD_EXIT|[58]|System.debug(ANY)
21:26:33.096 (96210000)|SYSTEM_METHOD_ENTRY|[59]|Database.query(String)
21:26:33.096 (96881000)|EXCEPTION_THROWN|[59]|System.QueryException: line 1:181 no viable alternative at character ' '
21:26:33.096 (96978000)|SYSTEM_METHOD_EXIT|[59]|Database.query(String)
21:26:33.097 (97076000)|SYSTEM_METHOD_ENTRY|[64]|ApexPages.addMessage(ApexPages.Message)
21:26:33.097 (97171000)|VF_PAGE_MESSAGE|Ooops!
21:26:33.097 (97202000)|SYSTEM_METHOD_EXIT|[64]|ApexPages.addMessage(ApexPages.Message)
21:26:33.097 (97266000)|METHOD_EXIT|[108]|01p900000008Gld|apposearchController.runQuery()
21:26:33.097 (97384000)|CODE_UNIT_FINISHED|apposearchController invoke(runSearch)
21:26:33.097 (97466000)|VF_APEX_CALL|j_id31|{!runSearch}|PageReference: none

 this is my code

public class apposearchController
{
    public List<Appointment1__c> Appoinment{get;set;}
    private String soql {get;set;}
    public String technologies { get; set; }
    
    public String sortDir 
    {
        get  { if (sortDir == null) {  sortDir = 'asc'; }  
       
        return sortDir;
      }
        
        set;
    }
    
    public String sortField {
    get  { if (sortField == null) {sortField = 'Appointment_Type__c'; } 
     
     return sortField; 
     }
   
    set;
    }
    
    public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
      }
      
    public apposearchController() {
    //delete(soql);
    soql = 'select Appointment_Type__c, Client__c, Start_Date__c, Status__c, Client_First_Name__c from Appointment1__c where Client_First_Name__c!= null';
    System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++Appoinment '+ Appoinment);
    System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++'+ soql  );
    runQuery();
      }
        
     public void toggleSort() 
    {
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
         
        runQuery();
        
    }

  //  public List<Appointment1__c> appo {get;set;}

    public String getStatus() {
        return null;
    }


    public void runQuery() {
         try
     {
     string a=soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20';
     system.debug(a);
      Appoinment = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
      System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++Appoinment '+ Appoinment);
     } 
    catch (Exception e) 
    {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
   }
   
    public PageReference runSearch() 
    {
    
     String AppoinmentType;
     String Clint;
     string StartDate;
     String Status;
     Date myDate;   
     
     AppoinmentType = Apexpages.currentPage().getParameters().get('AppoinmentType');
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++AppoinmentType '+ AppoinmentType);
     Clint = Apexpages.currentPage().getParameters().get('Clint');
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++Clint '+ Clint);
     
     StartDate = Apexpages.currentPage().getParameters().get('StartDate');
     if (!StartDate.equals(''))
     myDate = date.valueOf(StartDate);
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++StartDate '+ StartDate);
    // System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++myDate  '+ myDate );
     Status = Apexpages.currentPage().getParameters().get('Status');
 
    soql = 'select Appointment_Type__c, Client__c, Start_Date__c, Status__c, Client_First_Name__c from Appointment1__c where Client_First_Name__c != null';
   
    if (!AppoinmentType.equals(''))
      soql += ' and Appointment_Type__c  LIKE \''+String.escapeSingleQuotes(AppoinmentType)+'%\'';
      System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++AppoinmentType '+ AppoinmentType);
    if (!Clint.equals(''))
      soql += ' and Client_First_Name__c  LIKE \''+String.escapeSingleQuotes(Clint)+'%\'';
      System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++Clint '+ Clint);
     if (StartDate != null)
     // soql += ' and Start_Date__c = \'' + myDate + '\'';
     soql += ' and Start_Date__c  ='+ myDate ;
    //  soql += ' and Start_Date__c equals (\''+myDate+'\')';
    //  soql += ' and Client_First_Name__c  LIKE \''+date.escapeSingleQuotes(myDate)+'%\'';  
      System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++myDate '+ myDate);
     if (!Status.equals(''))
     soql += ' and Status__c LIKE \''+String.escapeSingleQuotes(Status)+'%\'';
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++Status'+ Status);
      
        runQuery();
        return null;
     }   
        public List<String> Status
        {
            get 
            {
            if (Status == null) 
            {
            Status = new List<String>();
            Schema.DescribeFieldResult field = Appointment1__c.Status__c.getDescribe();
     
            for (Schema.PicklistEntry f : field.getPicklistValues())
            Status.add(f.getLabel());
          }
          return Status;          
        }
        set;
        
    }

}

 please help me 

thanks 

Anuraj

RjSanchezRjSanchez

Your approach doesn't seem to work with calls to Database.Query().

I suspect it is because the date gets converted to a string before being passed to the query method of database.

String startDate = '2011-09-13';
Date myDate = Date.ValueOf(startDate);
List<myObject__c> myobj = new List<myObject__c>(); String soql = 'SELECT Id, LastModifiedDate FROM myObject__c WHERE LastModifiedDate = '; soql += myDate; myobj = Database.query(soql);

 I get the same error.

 

But if I approach it in the following way:

String startDate = '2011-09-13';
Date myDate = Date.ValueOf(startDate);
List<myObject__c> myobj = new List<myObject__c>();

myobj = [SELECT Id, LastModifiedDate FROM myObject__c WHERE LastModifiedDate = :myDate];

 I get a success.

 

From the looks of it, you are building the query string as you go so in order to convert it to the second approach would be messy but not impossible.

 

spraetzspraetz

I think you need to simply surround your date  with single quotes to make it a string.

 

String soql = 'SELECT Id, LastModifiedDate FROM myObject__c WHERE LastModifiedDate = \'';
soql += myDate;
soql += '\'';
myobj = Database.query(soql);



RjSanchezRjSanchez

Ryan,

Your suggestion was attempted earlier but I tried it once again and found this error, confirming my previous assessment.

 

10:09:19:027 EXCEPTION_THROWN [9]|System.QueryException: value of filter criterion for field 'LastModifiedDate' must be of type dateTime and should not be enclosed in quotes

Doesn't seem like you can use a string when comparing against a Date or DateTime fields.

 

Try this simple annonymous code do demonstrate the above error:

String startDate = '2011-09-13';
Date myDate = Date.ValueOf(startDate);
List<Contact> myobj = new List<Contact>();

String soql = 'SELECT Id, LastModifiedDate FROM Contact WHERE LastModifiedDate = \'';
soql += myDate;
soql += '\'';

myobj = Database.query(soql);