0

I have written vf page with input date filed. When I change input date the value needs to pass in query and compare with createddate then I want to display matching records in page.

I have done almost but when I am using Createddate >: dat then only records are displaying. When I used Createddate =:dat for that exact date it is not. I found reason for this.because Createddate is the date+time but we are passing date only. As a end user can not pass exact time with date. I want to display records based on user selected date automatically.

Code:

Page

<apex:pageBlockSection >
        <apex:pageBlockSectionItem >
            Date: <apex:input type="date" value="{!dat}" onChange="pickDate()"/>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
  <apex:pageBlockSection title="Criteria">

Controller

public class DisplayInputDateRecordsCtrl{

 public Date dat{get;set;}
 public List<Invoice__c> Invoices {get; set;}

 public PageReference executeSearch()
 {

  Invoices =[select id, createddate,Name, Invoice_Total__c,expirydate__c,(SELECT id, name, Quantity__c, Unit_Price__c FROM Line_Items__r)
            from Invoice__c 
            where createddate >: dat limit 20]; 
            //createddate =: dat   --->not displaing records

      return null;
  }
}
4
  • Two quick thoughts - you could add a formula field on Invoice__c that is DATEVALUE(Createddate) or have the comparison on createddate converted to date format using createddate.date().
    – Dave Humm
    Commented Sep 16, 2016 at 9:50
  • how can i convert to date format..?
    – Gopinath
    Commented Sep 16, 2016 at 10:00
  • Please share example i was confused
    – Gopinath
    Commented Sep 16, 2016 at 10:01
  • Just want to test the date() method to see if it will work.
    – Dave Humm
    Commented Sep 16, 2016 at 10:04

1 Answer 1

1

You can use DAY_ONLY (not an obvious name for a function that pulls the Date part from a Datetime) from the Date Functions in the where clause:

where DAY_ONLY(convertTimezone(CreatedDate)) > :dat

combined with convertTimezone that deals with Converting Time Zones in Date Functions.

See my colleague's blog post on this subject Gotcha: convertTimezone() must be used in SOQL Date functions dealing with Datetime for more detail.

4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .