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
Frank CarterFrank Carter 

visualforce page: <input type="date"/>

Hello, I'm trying to create a vf page and I need some help.
VF page:
<apex:page  controller="searchBDController" docType="html-5.0" id="pg">

<apex:form>
    <apex:pageBlock>
        <center><h1>Billing Details Search Page</h1></center>
        <br/>
        <br/>
        <br/>
       <center>
           Start Search: <input type="date"/>
   		   End Search: <input type="date"/>
       </center>
        <br/>
        
        <div align="center" draggable="false" >
			<apex:commandButton value="Reset" action="{!reset}"  />
            <apex:commandButton  value="Search" action="{!loadData}" style="text-align:left;" />
		</div>
          
        
            
    </apex:pageBlock>


        <apex:pageBlock id="pgblk">
 			<apex:pageBlockTable value="{!listBD}" var="b">
					<apex:column value="{!b.Name}"/>
                    <apex:column value="{!b.SF_Opportunity_Id__c}"/>
                    <apex:column value="{!b.Billing_Type__c}"/>
                    <apex:column value="{!b.Billing_Period__c}"/>    
                    <apex:column value="{!b.Monthly_Forecast__c}"/>
                    <apex:column value="{!b.End_of_Billing__c}"/>
                    <apex:column value="{!b.Amount__c}"/>
                    <apex:column value="{!b.Billing_Status__c}"/>
                    
                    
            </apex:pageBlockTable>

        </apex:pageBlock>

   


 </apex:form>
</apex:page>
Controller:
public class searchBDController {
 	
     public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [Select Id,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c ]));
                  
               
            }            
            return setCon;
        }
        set;
    }
    
     public List<Billing_Detail__c> listBD {get;set;}
    public String SelectedBdId {get;set;}
            
    public void loadData() {
        listBD = [Select Id,SF_Opportunity_Id__c,Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c ];
    }
    
    
   
    public PageReference reset(){
        PageReference pg = new PageReference(System.currentPageReference().getURL());
        pg.setRedirect(false);
        return pg;
    }
}
The target is when an user sets two dates and  clicks search button the page must give him all the records with Monthly_Forecast__c included between start search and end search <input type="date"/>. These 2 fields are not in the custom object Billing_Detail__c and must not be related with Billing_Detail__c.
I think is not possible use an <input type="date"/> in the controller query.
How I have to do??

Thanks,
Francesco
















 
Best Answer chosen by Frank Carter
Alain CabonAlain Cabon
Hi,

The samples in the documentation of Salesforce are sometimes too short and the missing parts are important (otherwise that doesn't work).
 
<apex:page standardController="Account" recordSetVar="accounts" extensions="FilterDates" docType="html-5.0">
    <apex:pageMessages />
    <apex:pageBlock title="Viewing Accounts">
        <apex:form id="theForm">
            <apex:actionRegion >
                <apex:outputLabel value="Start Date"/>
                <apex:input value="{!StartDate}" type="date" /><br/>
                <apex:outputLabel value="End Date"/>
                <apex:input value="{!EndDate}" type="date" />
                <apex:commandButton value="Select Dates" action="{!selectDates}" reRender="myPanel"/>             
            </apex:actionRegion>          
            <apex:panelGroup id="myPanel">                          
                <apex:pageBlockSection >
                    <apex:dataTable value="{!accountPagination}" var="acct" >
                        <apex:column headerValue="Name" value="{!acct.name}" />
                        <apex:column headerValue="Create Date" value="{!acct.createddate}" />  
                    </apex:dataTable>
                </apex:pageBlockSection>
                <apex:panelGrid columns="2">
                    <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                    <apex:commandLink action="{!next}">Next</apex:commandlink>
                </apex:panelGrid>
            </apex:panelGroup>          
        </apex:form> 
    </apex:pageBlock>
</apex:page>
 
public class FilterDates {
    private static Boolean changed {get;set;}
    public static Date StartDate {get;set;}
    public static Date EndDate {get;set;}
    private final Account acct;
    static {
        StartDate = Date.today()-1200;
        EndDate = Date.today();
        changed = false;
    }           
    public FilterDates(ApexPages.StandardSetController controller) {
        this.acct = (Account)controller.getRecord(); 
    }     
    public PageReference selectDates(){
        system.debug('StartDate:' + StartDate);
        system.debug('EndDate:' + EndDate);
        changed = true;
        return null;
    }    
    public PageReference next () {
        accountRecords.next();
        return null;
    }    
     public PageReference previous () {
         accountRecords.previous();
         return null;
    }    
    public ApexPages.StandardSetController accountRecords {
        get {
            if(accountRecords == null || changed) {
                changed = false;
                accountRecords = new ApexPages.StandardSetController(
                    Database.getQueryLocator([SELECT Name,CreatedDate FROM Account WHERE day_only(createddate) >= :StartDate and day_only(createddate) <= :EndDate order by createddate LIMIT 2000]));
            }
            return accountRecords;
        }
        private set;
    }
    public List<Account> getAccountPagination() {
        return (List<Account>) accountRecords.getRecords();
    }  
}

 

All Answers

Alain CabonAlain Cabon
Hi,

The samples in the documentation of Salesforce are sometimes too short and the missing parts are important (otherwise that doesn't work).
 
<apex:page standardController="Account" recordSetVar="accounts" extensions="FilterDates" docType="html-5.0">
    <apex:pageMessages />
    <apex:pageBlock title="Viewing Accounts">
        <apex:form id="theForm">
            <apex:actionRegion >
                <apex:outputLabel value="Start Date"/>
                <apex:input value="{!StartDate}" type="date" /><br/>
                <apex:outputLabel value="End Date"/>
                <apex:input value="{!EndDate}" type="date" />
                <apex:commandButton value="Select Dates" action="{!selectDates}" reRender="myPanel"/>             
            </apex:actionRegion>          
            <apex:panelGroup id="myPanel">                          
                <apex:pageBlockSection >
                    <apex:dataTable value="{!accountPagination}" var="acct" >
                        <apex:column headerValue="Name" value="{!acct.name}" />
                        <apex:column headerValue="Create Date" value="{!acct.createddate}" />  
                    </apex:dataTable>
                </apex:pageBlockSection>
                <apex:panelGrid columns="2">
                    <apex:commandLink action="{!previous}">Previous</apex:commandlink>
                    <apex:commandLink action="{!next}">Next</apex:commandlink>
                </apex:panelGrid>
            </apex:panelGroup>          
        </apex:form> 
    </apex:pageBlock>
</apex:page>
 
public class FilterDates {
    private static Boolean changed {get;set;}
    public static Date StartDate {get;set;}
    public static Date EndDate {get;set;}
    private final Account acct;
    static {
        StartDate = Date.today()-1200;
        EndDate = Date.today();
        changed = false;
    }           
    public FilterDates(ApexPages.StandardSetController controller) {
        this.acct = (Account)controller.getRecord(); 
    }     
    public PageReference selectDates(){
        system.debug('StartDate:' + StartDate);
        system.debug('EndDate:' + EndDate);
        changed = true;
        return null;
    }    
    public PageReference next () {
        accountRecords.next();
        return null;
    }    
     public PageReference previous () {
         accountRecords.previous();
         return null;
    }    
    public ApexPages.StandardSetController accountRecords {
        get {
            if(accountRecords == null || changed) {
                changed = false;
                accountRecords = new ApexPages.StandardSetController(
                    Database.getQueryLocator([SELECT Name,CreatedDate FROM Account WHERE day_only(createddate) >= :StartDate and day_only(createddate) <= :EndDate order by createddate LIMIT 2000]));
            }
            return accountRecords;
        }
        private set;
    }
    public List<Account> getAccountPagination() {
        return (List<Account>) accountRecords.getRecords();
    }  
}

 
This was selected as the best answer
Alain CabonAlain Cabon

Building a Custom List Controller​: should be fixed (?)
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_custom_list_controller.htm

I based my code on their sample.

<apex:page standardController="Account" recordSetVar="accounts" extensions="AccountPagination">
<apex:dataList value="{!accountPagination}" var="acct" type="1">

Their sample cannot work correctly. Many people can waste a lot of time with this kind of wrong/too short code given by Salesforce (?) and conclude that it is impossible.
Frank CarterFrank Carter
Yes I edited my code with your. 

Now with the range dates it's perfect.
vf:
<apex:page controller="searchBDController" docType="html-5.0" id="pg">

<apex:form >
    <apex:pageBlock title="Billing Details Search Page" >
        <br/>
        <br/>
       <apex:actionRegion >
           <center>
                <apex:outputLabel value="Start Date"/>
                <apex:input value="{!StartDate}" type="date" /><br/>
                <apex:outputLabel value="End Date "/>
                <apex:input value="{!EndDate}" type="date" /> 
               </center>
            </apex:actionRegion>  
    <br/>
        
        	<div align="center" draggable="false" >            
                <apex:commandButton value="Search" action="{!loadData}" style="text-align:left;" />
                
                <apex:commandButton value="Moves to next month" style="text-align:left;" onclick="if(!confirm('If you click ok all the billing details you are viewing will be paid for next month. Are you sure? ')) return false;" action="{!myAction}" />  
			</div>

        
            
    </apex:pageBlock>


        <apex:pageBlock id="pgblk">
 			<apex:pageBlockTable value="{!listBD}" var="b">
					<apex:column value="{!b.Name}"/>
                    <apex:column value="{!b.SF_Opportunity_Id__c}"/>
                    <apex:column value="{!b.Billing_Type__c}"/>
                    <apex:column value="{!b.Billing_Period__c}"/>    
                    <apex:column value="{!b.Monthly_Forecast__c}"/>
                    <apex:column value="{!b.End_of_Billing__c}"/>
                    <apex:column value="{!b.Amount__c}"/>
                    <apex:column value="{!b.Billing_Status__c}"/>
                    <apex:column >
                    <apex:outputLink title="" value="/{!b.Id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">EDIT</apex:outputLink>
                    </apex:column>
                    
            </apex:pageBlockTable>

        </apex:pageBlock>

   


 </apex:form>
</apex:page>

Controller:
public class searchBDController {
    
    public static Date StartDate {get;set;}
    public static Date EndDate {get;set;}
    private static Boolean changed {get;set;}
 
     public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [Select Id,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c Where Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate Order by Monthly_Forecast__c]));
                  
               
            }            
            return setCon;
        }
        set;
    }
    
     public List<Billing_Detail__c> listBD {get;set;}
    public String SelectedBdId {get;set;}
            
    public void loadData() {
        listBD = [Select Id,SF_Opportunity_Id__c,Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c Where Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate Order by Monthly_Forecast__c];
    }
    
    //to move monthly forecast
 	public PageReference myAction(){
		MoveBD1.Move1();
        PageReference pageRef = ApexPages.currentPage();
      	pageRef.setRedirect(true);
      	return pageRef;
	} 
   

}

Now I have another problem, this is the vf page:
User-added image

I have inserted a range of date.
If I go to edit a record it opens the record and I have three button:
- save;
- cancel;
- save and new.

I want that if I click save or cancel it redirect me on the previus page with the two filter date filled instead of the filetrs empties (to fill).

Is it possible?

Thanks


 
Alain CabonAlain Cabon
Hello,

If you want a partial page refresh, you need to use the  "rerender" attribute as far as possible with the Ajax underlying used technique by Salesforce. 

I used "reRender" if you look at my posted code to keep the values of the filter dates  (not emptied) and here is a detailed explaination:

https://developer.secure.force.com/cookbook/recipe/using-ajax-in-a-visualforce-page

Best regards
Alain
Frank CarterFrank Carter
Hello,
Thanks for answer me. I Understand but I don't think what I want is a partial refresh. If I open the record and then I click Save ord Cancel I want the previous page with the fields data range filled and the result of the query
User-added image
Example If I go in the first result, last column edit:
I obtain the record page with the 3 butons (save, save and new, cancel).

If I clicks Save or Cancel I want the same page.
User-added image

this page of the reult have to change only if I change the data range or If I close the page and Open from scratch.
Is it possible?

Thanks
 
Alain CabonAlain Cabon
Without the "rerender" technique (Ajax) because you need to "refresh" the entier page, you can to "save" temporarely the values somewhere and there is the view state directly by removing "static" but there is the problem of the first initialization also done during the refreshing of the page. 

The keyword static optimizes the size used for the view state by removing it from the view state but not always wanted.

public static Date StartDate {get;set;}
public static Date EndDate {get;set;}
 
Alain CabonAlain Cabon
You can test during the first initialization (constructor) if the dates are empty (null) and fill them with the default values.
Frank CarterFrank Carter
Ok But I can set the as default values  the last range dates that I used?
I don't want a static default value I want the last data range.
So If I understand well I remove static from the initialzation of start date , end date then I use a constructor method like your to set the default value (last data range).

Is it correct? I'll try and then I'll let you know.


Thanks
Frank CarterFrank Carter
sorry but I don't understand how to do. Can you hel me?