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 

Search date

Hi

In this code I have a text box where i have to enter the date and the code is for searching the date from the object.

 

The code is 

 

Date myDate = date.valueOf(StartDate);
if (!StartDate.equals(''))
 soql += ' and Start_Date__c LIKE (\''+myDate+'\')';


if the format is in year-month-date then the error
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));

and if any other format is used the the error is :

System.TypeException: Invalid date: 2011-16-9

 

thanks 

Anuraj

bob_buzzardbob_buzzard

This will look for the string representation of the date in the field - is that what you are looking for, i.e. is Start_Date__c a text field?  

 

Can you explain a little more about what you are trying to achieve?

anurajanuraj

Hi bob

Yes this is looking for text. I converted it to date type. As this is a date field I have removed the ' ' and done it as

if (!StartDate.equals(''))
    
     myDate = date.valueOf(StartDate);
 soql += ' and  Start_Date__c = ' + myDate;

 but now the problem is when i changed it to date it is in

datetime format

eg : 2011-09-20 00:00:00

and the code is showing error for this I used this code substring but not working

 newdate = myDate.substring(0,10);

 I run the query in Apex explore with out 2011-09-20 it is giving the result. 

Now i want to remove time from the mydate value

please help me.

 

Thanks

Anuraj

 

bob_buzzardbob_buzzard

So if I understand correctly, you have a text representation of a Date in the StartDate variable.  That being the case, why are you converting it to a date and back again?

anurajanuraj

Hi bob

Leave this line of code

newdate = myDate.substring(0,10);

 I have commended it. 

I want to get only date from myDate 

Now i am getting both date and time

 

thanks 

Anuraj

bob_buzzardbob_buzzard

What is the format of your startdate string?  There may be no reason to go via a Date variable.

 

anurajanuraj

This is the code I wrote

public PageReference runSearch() 
    {
    
     String AppoinmentType;
     String Clint;
     string StartDate;
     String Status;
     Date myDate; 
     datetime newdate;  
     
     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);
    // newdate = myDate.substring(0,10);
   //  trim(myDate);
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++StartDate '+ StartDate);
     System.debug('++++++++++++++++++++++++++++++++++++++++++++++++++++++++newdate '+ newdate);
     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 + '' ;
     //integer year = myDate.year();
    // integer month = myDate.month();
    // integer day = DAY_ONLY(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;
     }   

I have not set any format it is takeing as year/month/day

 

 thanks

Anuraj

bob_buzzardbob_buzzard

What is the format of your date string?