Sunday 30 December 2012

Searching the records Based on Date Selection:-

Here we have a scenario ,i.e Create a Date Field in vf page, create one search button 
When we select the date and hit the button. Then it will search all records which is created
on selected date.


<apex:page controller="exampletest">
<apex:form >
       <script>
        function DynamicDatePicker(d_id)
        {
            DatePicker.pickDate(true,d_id.id,false);
        }
        window.onload=function() {
            // prevent autopup of the date inputfield by the default focus behavoir
            document.getElementById('focusDistraction').focus();
        }
        </script>
      <apex:pageBlock >
            <apex:pageMessages > </apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Schedule}" value="Selected records"/>
            </apex:pageBlockButtons>
               <b>Date </b><apex:inputText value="{!sdate}" id="time" onfocus="DynamicDatePicker(this);" size="20" disabled="false" style="width:80px;"/>                
         
      <apex:pageBlockSection title="Result" columns="1">
          <apex:pageBlockTable value="{!Schedule}" var="a">
              <apex:column headerValue="Account ID" value="{!a.id}"/>
              <apex:column headerValue="Account Name" value="{!a.name}"/>
          </apex:pageBlockTable>
      </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
</apex:page>

Apex class:-
-------------
public class exampletest {
    public Date sdate{get;set;}
    Datetime sdt;
    Datetime edt;
    public string var1{get;set;}
    public string var2{get;set;}
    public List<Account> var;
    public void Schedule(){
             Time t=Time.newInstance(0,0, 0, 0);
             Time t1=Time.newInstance(23,59, 59, 50);
             if(sdate==null)
             {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Scheduled Date/Time is Required!!'));
                 return;
             }
             sdt=Datetime.newInstance(sdate,t);
             edt=Datetime.newInstance(sdate,t1);
             System.debug(edt+'=======sddddddddddddddttttt======'+sdt);
            
              var=[select id,name,createddate from account where createddate <=:edt and createddate>=:sdt];
              System.debug('=======sixzeee======'+var.size());
             if(var.size()!=0){
                 for(integer i=0;i<var.size();i++)
                 {
                    System.debug('======nemes ============='+var[i].name);
                    var1=var[i].id;
                    var2=var[i].name;
                 }
             }
        }
        public List<Account> getSchedule(){
            return var;
        }
    
}

No comments:

Post a Comment