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
Shawn Reichner 29Shawn Reichner 29 

Date format changes when used on Visualforce Page Search

Hello awesome devs!

I am attempting to create a VF page with 4 fields for user inputting.  2 of these fields are Date fields which when filled in like MM/DD/YYY when I press the search button to call my custom controller the date in the field populated has its format changed to Tue Dec 26 00:00:00 GMT 2017 and doesnt seem to be being applied in the search itself as all records are returned and not ones just for the date entered. 

Can someone help me with gettign the format to stay as I think this is the cause of the date field not being used in the search criteria. 

Please see my VF Page and controller code below. 

Thank you very much for any help you can provide,

Shawn

VF Page Code - 
 
<apex:page controller="SubSearchController">
    
    <apex:form>
    	<apex:pageBlock id="pb">
        	<apex:pageBlockButtons>
                <apex:commandButton action="{!searchRecords}" value="Search" rerender="pb"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:outputLabel value="Account Id" />
                <apex:inputText value="{!AccId}"/>
                <apex:outputLabel value="Status" />
                <apex:inputText value="{!SubStatus}"/>
                <apex:outputLabel value="Service Activation date" />
                <apex:inputText value="{!SAD}"/>
                <apex:outputLabel value="Term End Date" />
                <apex:inputText value="{!TermEnd}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable var="record" value="{!records}" id="pbTable">
                <apex:column value="{!record.Name}" />
                <apex:column value="{!record.Zuora__Status__c}" />
                <apex:column value="{!record.Zuora__ServiceActivationDate__c}" />
                <apex:column value="{!record.Zuora__TermEndDate__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Controller Code -

public class SubSearchController {

    public String AccId {get;set;}
    public String SubStatus {get;set;}
    public Date SAD {get;set;}
    public Date sadDate = Date.valueOf(SAD);
    public Date TermEnd {get;set;}
    public Date TermDate = Date.valueOf(TermEnd);
    
    public List<Zuora__Subscription__c> records {get; private set;}
    
    public SubSearchController(){
        records = new List<Zuora__Subscription__c>();   
    }
    
    public PageReference searchRecords() {
        
        records = [SELECT Zuora__Account__c, Zuora__Status__c, Zuora__ServiceActivationDate__c, Zuora__TermEndDate__c, OpportunityId__c, Total_Booking_Amount__c, Id, Name
                  FROM Zuora__Subscription__c WHERE Zuora__Account__c = : AccId AND Zuora__Status__c = : SubStatus AND (Zuora__ServiceActivationDate__c = : sadDate OR Zuora__TermEndDate__c = :TermDate)];
        return null;
        
    }
    
}

 
Best Answer chosen by Shawn Reichner 29
badibadi
Shawn,

The  <apex:inputText > is converting the date to text format. So using <apex:input type="date"> should solve the problem

use 
<apex:page controller="testing7" docType="html-5.0">


<apex:outputLabel value="Subscription Date" />
                <apex:input type="date" value="{!SAD}" />
                <apex:outputLabel value="Issue Date" />
                <apex:input type="date" value="{!TermEnd}"/>

All Answers

badibadi
Change this line 
<apex:column value="{!record.Zuora__TermEndDate__c}" />

to
<apex:column>
	<apex:outputField value="{!record.Zuora__TermEndDate__c}" />
</apex:column>
if its a custom field. because <apex:outputField> component respects the attributes of the associated field, including how it should be displayed to the user. In this case, the value will be formatted as date. 

or use, 
<apex:column>
      <apex:outputText value="{0,date,MM/dd/yy}"> 
              <apex:param value="{!record.Zuora__TermEndDate__c}" /> 
       </apex:outputText> 
</apex:column>
Shawn Reichner 29Shawn Reichner 29
badi, thank you for your advice, however both methods did not change the behavior.  I am still gettign all records and the date is being formatted to the longer date string still as well.  Any other thoughts?  
kiranmutturukiranmutturu
try using TermDate.format() in the query and same to be applied to all the other date references. 
Shawn Reichner 29Shawn Reichner 29
Kiran, I have tried your approach and I receive the followign error when attempting to add the .format()  

Invalid bind expression type of String for column of type Date.   Any idea how to get around that?

Shawn
badibadi
Shawn,

The  <apex:inputText > is converting the date to text format. So using <apex:input type="date"> should solve the problem

use 
<apex:page controller="testing7" docType="html-5.0">


<apex:outputLabel value="Subscription Date" />
                <apex:input type="date" value="{!SAD}" />
                <apex:outputLabel value="Issue Date" />
                <apex:input type="date" value="{!TermEnd}"/>
This was selected as the best answer
kiranmutturukiranmutturu
Shawn, Sorry for the confusion I mean to say you should add that to the query Zuora__TermEndDate__c = :TermDate.format();