+ Start a Discussion
OssieOssie 

The Complexities of DateTime Fields

Hi,

 

I have a search form whereby i can search by account name and results are displayed from the case object.  I would now like to search using a start date but i cannot get this to work i.e no results are returnedand no ERROR message displayed!!

 

Can someone plz tell me what i might doing wrong?  THANKS in advance!! 

 

<apex:page id="Cas" controller="CaseController9" sidebar="false">

    <apex:form >
    <apex:pageBlock >      

            <apex:pageBlockSection columns="2">
               
                <apex:pageBlockSectionItem >
                    <apex:outputText value="Account Name"/>
                    <apex:inputText id="accountName" value="{!accountName}"/>
                 </apex:pageBlockSectionItem>
                 
                <apex:pageBlockSectionItem >
                    <apex:outputText value="Start  Date"/>
                    <apex:inputText id="startDate" value="{!startDate}"/>
                 </apex:pageBlockSectionItem>
                                       
            </apex:pageBlockSection>
                       
            <p></p>
            <table>
                <tr>
                <td align="bottom" valign="center">
                    <br></br>
                    <apex:commandButton action="{!search}" value="Search" id="searchBtn" status="Searching"/>                    
                 </td>
                 </tr>
             </table>
    
    <apex:pageBlock tabStyle="Case" id="pagin" rendered="{!IF(Result.size > 0 , true , false)}">
     
        <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav2">
        
        Total Records Found :&nbsp; <apex:outputText rendered="{!IF(Con.resultSize==10000,true,false)}" >10000 +</apex:outputText><apex:outputText rendered="{!IF(Con.resultSize < 10000,true,false)}">{!Con.resultSize}</apex:outputText>
            <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>
            <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>

        <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" reRender="pagin" rendered="{!Con.HasPrevious}"/>

        <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasPrevious)}">Previous Page</apex:outputPanel>&nbsp;
        ({!IF(Con.PageNumber == 1,1,((Con.PageNumber -1) * Con.PageSize)+1)}-{!IF(Con.resultSize < Con.PageSize,Con.resultSize,Con.PageNumber * Con.pageSize)})&nbsp;
        <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(Con.HasNext)}">Next Page</apex:outputPanel>

        <apex:commandLink title="Next Page" value="Next Page" reRender="pagin" rendered="{!Con.HasNext}" action="{!Next}"/>&nbsp;
            <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>
            <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>

        </apex:outputPanel>
        <br></br>

    <apex:pageBlock mode="edit" id="Results">
        <apex:pageBlockTable value="{!result}" var="cas" id="data">
           
            <apex:column headerValue="Start Date" value="{!cas.CreatedDate}"/>
            <apex:column headerValue="Account Name" value="{!cas.Account.Name}"/>

        </apex:pageBlockTable>
    </apex:pageBlock>    
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Public with Sharing Class CaseController9 {

    public String accountname{get;set;}
    public DateTime startDate{get;set;}        
    
    public List<Case> Result
    {
        get  
        {  
            if(con != null)  
                return (List<Case>)con.getRecords();  
            else 
                return null;  
        }  
        set;
        }        
     
    public PageReference search() {
        
        //Search using field Account Name only
        If (startDate==Null && accountname!='')
        {  
            con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT account.name, createddate FROM Case WHERE Account.Name LIKE :accountname+'%' limit 100]));
            con.setPageSize(200); 
        }
         //Search using field Start Date only
        If (startDate!=Null && accountname=='')
        {  
            con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT account.name, createddate FROM Case WHERE CreatedDate = :startDate limit 100]));
            con.setPageSize(200); 
        }
           else
        {    
            con = null;
        }         
            return null;        
    }   
             
        public ApexPages.StandardSetController con { get; set; }
        
        public CaseController9()
        {            
            con = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT CreatedDate, account.name FROM Case Order By account.name]));
        }            
    
    public Boolean hasNext
    {
        get
        { return con.getHasNext(); }
        set;
    }        
    
       public Boolean hasPrevious
    {
        get
        { return con.getHasPrevious(); }
        set;
    }    
    
     public Integer pageNumber  
    {  
        get  
        { return con.getPageNumber(); }  
        set; 
    }    
    
    public void next()
    { con.next(); }

    public void previous()  
    { con.previous(); }    
}

 

kpeterson85kpeterson85

CreatedDate is a datetime field.  What kind of value are you entering for StartDate?  Does it include just a date or a time as well?  If you are wanting to view records for an entire day then you'll need to query for just the day, not a specific day and time of the day.

OssieOssie

I would like to enter just a date and not worry about the time.  for example, i would like to enter DD/MM/YYYY and any Case records which match the date (ignoring the time) should be returned.

kpeterson85kpeterson85

Try changing your startdate variable to be a date type instead of datetime.

hisrinuhisrinu

Couple suggestions.

1. If you use a inputfield with date as a data type then you will get the date format automatically

2. If you want user to enter/pick only the date then in your logic you need to frame the query as createddate>2010-06-20T00:00:00.000Z and createddate<2010-06-20T23:59:59.000Z

 

Hope this solves your problems