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
james7630james7630 

VF Page to use date field to filter query for datatable

I want to have a date field on a page above my datatable where I can click a date and that date is then used in the query that populates the datatable objects. I have the date field and the datatable but cant seem to get them working together. I can populate the datatable on intial load by hard coding  date,today for the query. 

 

public class RenewalController
{

    public RenewalController(ApexPages.StandardController controller) 
    { }


    public String getDate() 
    {
        return selectedAccount;
    }


public String selectedAccount { get; set; }  

public  List<Entities__c> myents = new List<Entities__c>();

    public List<Entities__c> getMyList() 
    {
    date dt;
    if(getDate()!= null)
        dt = Date.parse(getDate());
    else
        dt = date.Today();
        
        integer mo = dt.month();
 
        myents = [select id,Packet_Sent__c,Stamped_List_Received_from_State__c,Renewal_Emailed__c ,List_Filed_with_State__c,Received_Payment_For_Renewal__c,Renewal_Received_From_State__c ,State_Renewal__c,Renewal_Printed__c, Primary_Client__c, Name from Entities__c where (CALENDAR_MONTH(State_Renewal__c) =: mo)];
   
       return myents;
    }
 
 }

 page

<apex:page standardController="Entities__c" extensions="RenewalController" tabStyle="Entities__c" sidebar="false">
  <apex:form >
  <input id="datefield1" name="datefield1" onfocus="DatePicker.pickDate(false, 'datefield1', false);" size="12" type="text" value="">
  <apex:param name="id" value="{!Date}" assignTo="{!selectedAccount}"/>
  </input>
    <apex:pageBlock title="Entity" mode="edit" id="ent">
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" rerender="ents" status="ajaxStatus"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
        <apex:commandButton value="AutoFillDate" action="{!AutoFill}" rerender="ents" status="ajaxStatus"/>
        
      </apex:pageBlockButtons>
      
      <apex:dataTable value="{!myList}" var="opp" id="ents">
      <apex:actionSupport event="onchange"
                      rerender="ent"
                      status="status"/>

      <apex:column headerValue="Entity">{!opp.Name}&nbsp;</apex:column>
        <apex:column headerValue="Renewal Due Date">
        <apex:inputField value="{!opp.State_Renewal__c}" id="renew"/>
        </apex:column>
        <apex:column headerValue="Renewal Received">
        <apex:inputField value="{!opp.Renewal_Received_From_State__c}"/>
        </apex:column>
        <apex:column headerValue="Printed Renewal">
         <apex:inputField value="{!opp.Renewal_Printed__c}" id="print"/>
        </apex:column>
        <apex:column headerValue="Emailed Renewal">
        <apex:inputField value="{!opp.Renewal_Emailed__c}"/>
        </apex:column>
        <apex:column headerValue="Rec Pay For Renewal">
        <apex:inputField value="{!opp.Received_Payment_For_Renewal__c}"/>
        </apex:column>
        <apex:column headerValue="List Filed With State">
        <apex:inputField value="{!opp.List_Filed_with_State__c}"/>
        </apex:column>
        <apex:column headerValue="Stamped List Received">
        <apex:inputField value="{!opp.Stamped_List_Received_from_State__c}"/>
        </apex:column>
        <apex:column headerValue="Packet Sent">
       <apex:inputField value="{!opp.Packet_Sent__c}"/>
        </apex:column>
      </apex:dataTable>
     
      <script>
          function CopyShipInfo()
          {
            document.getElementById('{!$Component.ents.renew}').value = document.getElementById('datefield1').value;
        </script>
         
    </apex:pageBlock>
  </apex:form>
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
james7630james7630

All Answers

@anilbathula@@anilbathula@

HI

 

  • You can use a date function in a WHERE clause to filter your results even if your query doesn't include a GROUP BY clause. The following query returns data for 2009:
     
    SELECT CreatedDate, Amount
    FROM Opportunity
    WHERE CALENDAR_YEAR(CreatedDate) = 2009
  • You can't compare the result of a date function with a date literal in a WHERE clause. The following query doesn't work:
    SELECT CreatedDate, Amount
    FROM Opportunity
    WHERE CALENDAR_YEAR(CreatedDate) = THIS_YEAR
  • You can't use a date function in a SELECT clause unless you also include it in the GROUP BY clause. There is an exception if the field used in the date function is a date field. You can use the date field instead of the date function in the GROUP BY clause. This doesn't work for dateTime fields. The following query doesn't work because CALENDAR_YEAR(CreatedDate) is not in a GROUP BY clause:
    SELECT CALENDAR_YEAR(CreatedDate), Amount
    FROM Opportunity

    The following query works because the date field, CloseDate, is in the GROUP BY clause. This wouldn't work for a dateTime field, such as CreatedDate.

     
    SELECT CALENDAR_YEAR(CloseDate)
    FROM Opportunity
    GROUP BY CALENDAR_YEAR(CloseDate)

     

 

In your query use group by  or order by clause.

check this link how to use calender_month():-

http://blogs.developerforce.com/tech-pubs/2011/12/calculation-fields-in-summary-soql-queries.html

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_date_functions.htm

james7630james7630

Thanks, but thats not my issue. My issue is that I cant get that variable for the date field from the page to the controller and into the query to rerender the datatable. I just want to be able to select a date on the page and that will cause the datatable to rerender and give me a datatable with just those objects within that date range.

 

Thanks in advance..

bob_buzzardbob_buzzard

The way that you are sending the selected value to the controller looks wrong to me:

 

<input id="datefield1" name="datefield1" onfocus="DatePicker.pickDate(false, 'datefield1', false);" size="12" type="text" value="">
  <apex:param name="id" value="{!Date}" assignTo="{!selectedAccount}"/>
  </input>

 

 

I can't see how the apex:param will help here, as its not nested in a visualforce component.

 

Take a look at this blog post of mine regarding custom date pickers - http://bobbuzzard.blogspot.co.uk/2012/03/custom-date-picker.html - that has some sample code that shows how to bind to a controller property.

 

james7630james7630

Hi Bob,

Thanks for the reply, I think I found something on one of your other posts about rerendering but now it isnt showing up at all.

I added a nested Output panel and I am trying to render the inner and rerender the outer container but nothing shows up, can you see any errors in the page or controller? I am using the autofill button to initiate the update after i fill in a date. Thanks again..

 

<apex:page standardController="Entities__c" extensions="RenewalController" tabStyle="Entities__c" sidebar="false">
  <apex:form >
    <apex:pageBlock title="Entity" mode="edit" id="ent">
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" rerender="ents" status="ajaxStatus"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
        <apex:commandButton value="AutoFillDate" action="{!myList}"/>     
      </apex:pageBlockButtons>
      
      <apex:outputPanel id="datePanelContainer">
          <input id="datefield1" name="datefield1" onfocus="DatePicker.pickDate(false, 'datefield1', false);" size="12" type="text" value="">
              <apex:param name="id" value="{!Date}" assignTo="{!selectedAccount}"/>
          </input>
       <apex:outputPanel id="datePanelInnerContainer"
       rendered="datePanelInnerContainer">
       
      <apex:dataTable value="{!myList}" var="opp" id="ents">
      <apex:actionSupport event="onchange"
                      rerender="datePanelInnerContainer"
                      status="status"/>

      <apex:column headerValue="Entity">{!opp.Name}&nbsp;</apex:column>
        <apex:column headerValue="Renewal Due Date">
        <apex:inputField value="{!opp.State_Renewal__c}" id="renew"/>
        </apex:column>
        <apex:column headerValue="Renewal Received">
        <apex:inputField value="{!opp.Renewal_Received_From_State__c}"/>
        </apex:column>
        <apex:column headerValue="Printed Renewal">
         <apex:inputField value="{!opp.Renewal_Printed__c}" id="print"/>
        </apex:column>
        <apex:column headerValue="Emailed Renewal">
        <apex:inputField value="{!opp.Renewal_Emailed__c}"/>
        </apex:column>
        <apex:column headerValue="Rec Pay For Renewal">
        <apex:inputField value="{!opp.Received_Payment_For_Renewal__c}"/>
        </apex:column>
        <apex:column headerValue="List Filed With State">
        <apex:inputField value="{!opp.List_Filed_with_State__c}"/>
        </apex:column>
        <apex:column headerValue="Stamped List Received">
        <apex:inputField value="{!opp.Stamped_List_Received_from_State__c}"/>
        </apex:column>
        <apex:column headerValue="Packet Sent">
       <apex:inputField value="{!opp.Packet_Sent__c}"/>
        </apex:column>
      </apex:dataTable>
     </apex:outputPanel>
    </apex:outputPanel>
      <script>
          function CopyShipInfo()
          {
            document.getElementById('{!$Component.ents.renew}').value = document.getElementById('datefield1').value;
        </script>
         
    </apex:pageBlock>
  </apex:form>
</apex:page>

 

public class RenewalController
{
public  List<Entities__c> myents = new List<Entities__c>();
    public PageReference myList() 
    {
        PageReference p = new PageReference('/apex/Renewals');
        p.setRedirect(true);
       getMyList();
       return p;
    }

    public RenewalController(ApexPages.StandardController controller) 
    { }


    public String getDate() 
    {
        return selectedAccount;
    }


public String selectedAccount { get; set; }  


    public List<Entities__c> getMyList() 
    {
    date dt;
    if(getDate()!= null)
        dt = Date.parse(getDate());
    else
        dt = date.Today();
        
        integer mo = dt.month();
 
        myents = [select id,Packet_Sent__c,Stamped_List_Received_from_State__c,Renewal_Emailed__c ,List_Filed_with_State__c,Received_Payment_For_Renewal__c,Renewal_Received_From_State__c ,State_Renewal__c,Renewal_Printed__c, Primary_Client__c, Name from Entities__c where (CALENDAR_MONTH(State_Renewal__c) =: mo)];
        
        
    
       return myents;
    }

 

james7630james7630
This was selected as the best answer