You need to sign in to do that
Don't have an account?
Nervosa
How can i create a query to search by date?
Greetings to everyone!
I've got a problem that had most of you, i think =)
There is an inputtext on my VF page:
<apex:inputText value="{!NewItemReleaseDate}" id="rdate" label="Release Date" onfocus="DatePicker.pickDate(false, this, false);"/>
As you can see users enter date here.
Also there is a PageBlockSection that function as a search block:
<apex:pageBlockSection title="Search an item by name and\or release date" collapsible="false"> <apex:pageBlockSectionItem > <apex:panelGrid > <apex:outputLabel style="float:left">Name</apex:outputLabel> <apex:inputText id="searchText" value="{!searchText}" style="float:left"/> <apex:outputLabel style="float:left" >Date</apex:outputLabel> <apex:inputText id="searchDate" value="{!searchDate}" onfocus="DatePicker.pickDate(false, this, false);" style="float:left"> <apex:param value="{0,date,MM/dd/yyyy}" assignTo="{!searchDate}"/> </apex:inputText> <apex:commandButton id="SearchButton" value="Search" action="{!ViewData}" reRender="FullFunctionalityForm"> <apex:param value="WHERE name=" assignTo="{!searchStr}"/> <apex:param value="" assignTo="{!searchDate}"/> </apex:commandButton> </apex:panelGrid> </apex:pageBlockSectionItem> </apex:pageBlockSection>
Here is a search method from my APEX controller:
public PageReference ViewData() { string sortFullExp = sortExpression + ' ' + sortDirection; if(searchText != null) { searchstr = 'WHERE Name LIKE \'%' + searchText + '%\' order by ';} if(searchDate != null) { searchstr = 'WHERE Name LIKE \'%' + searchText + '%\' and CreatedDate =: searchDate order by ';} items = Database.query('Select id, Name, Item_Price__c, CreatedDate from Item__c ' + searchstr + sortFullExp); return null; }
...and it doesn't work!!! =(
I guess that my issue concerns date formats both that entered when user adds new item and when searching one.
Help me please!
Thanks in advance.
how you havr declared your date value means as string if yes please convert your string value to date using following command
Date mycreatedDate=date.valueOf(SearchDate);
and then pass it to query. And also please write a debug line which will give you SOQL query line. And then execute the same query line in your debug console.
Thank you for reply.
I tried it, but it didn't fix the bug.
I'd like to trace CreatedDate and searchDate variables, but i don't know how to do it =(
Here is my debug log of a try of search by release date:
It seems that CreatedDate and searchDate have different formats of date though both added by calendar DatePicker.
Oops, sorry. CreatedDate - isn't custom field. So i don't know its format.
public PageReference ViewData() {
date mysearchdate=date.valueOf(searchDate);
string sortFullExp = sortExpression + ' ' + sortDirection; if(searchText != null) { searchstr = 'WHERE Name LIKE \'%' + searchText + '%\' order by ';} if(searchDate != null) { searchstr = 'WHERE Name LIKE \'%' + searchText + '%\' and CreatedDate >=: mysearchdate order by ';} items = Database.query('Select id, Name, Item_Price__c, CreatedDate from Item__c ' + searchstr + sortFullExp); return null;
}
sure this will help you.
Thanks a lot - it works, but only when condition is CreatedDate >=: mysearchdate and doesn't work for CreatedDate =: mysearchdate. Why is it so?