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
KawaiimomoKawaiimomo 

DatePicker not saving value

I am trying to make a page where the user can select start and end date, then I use those dates for getting a custom object transaction__c which Open_Date__c field is between them.

This is how I create the DatePicker:

 

<input value="{!startDate}" id="start" onfocus="DatePicker.pickDate(false,'start', false);" size="12" type="text" />

 

Then in the controller I have:

 

private Date startDate {get; set;}

 

and in the constructor

    cam = [SELECT Id, Name, StartDate, EndDate FROM Campaign WHERE Id = :controller.getRecord().Id] ;

    startDate = cam.StartDate;

 

    if (startDate == null) {

        startDate = Date.today();

    }

 

The getter is called because the date is set properly on the picker. However the setter is not being called, ie the picked date is not saved on my variable. I even created a setter and getter and can clearly see the getter is called, but not the setter.

 

So, is there anything else I need to do to save the picked date?

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Sometimes, value in HTML <input/> tag does not binds with controller. Try apex:inputField for getting date picker.

You can use instance of any object where you have date field.
Suppose, For an instance opportunity has close date.

// In Controller 
public Opportunity Opportunity1 {get;set;}
public Opportunity Opportunity2 {get;set;}
public constructor {
    Opportunity1.closeDate = date.today();
    Opportunity2.closeDate = date.today();
}

// In visualforce page

<apex:inputField value="{!Opportunity1.closeDate}" required="false">
<apex:inputField value="{!Opportunity2.closeDate}"  required="false">

// Just added required false so as to get rid of required validation.

 Let me know if you face any issues.

All Answers

Shiv ShankarShiv Shankar

1. First thing you have declared your  startDate variable as private property.

2. Second thing you have to specify Set method to set the value. because input field not tightly coupled with input field here.

3. Or you can use ApexPages.currentPage().getParameters().get('id') to access the value in controller.

KawaiimomoKawaiimomo

Thanks for replying Shiv, startDate variable was public before, then I made private in order to create a set and get method as shown below.

public void setStartDate(Date d) {
//SETTER IS NOT CALLED FROM PICKER
System.debug('SET START DATE TO ' + d);
startDate = d;
}

public Date getStartDate() {
System.debug('GET START DATE ' + startDate);
return startDate;
}


I created the setter and getter just to make sure the variable wasn't being set, and in fact the setted is not being called. Can you please clarify what do you mean by your point 2 and 3? I have a setter for my private variable and I tried before with a public variable without setter with the same result. Do I need to explicitly call the setter from the picker? If yes how to do so? Thanks again

Rahul SharmaRahul Sharma

Sometimes, value in HTML <input/> tag does not binds with controller. Try apex:inputField for getting date picker.

You can use instance of any object where you have date field.
Suppose, For an instance opportunity has close date.

// In Controller 
public Opportunity Opportunity1 {get;set;}
public Opportunity Opportunity2 {get;set;}
public constructor {
    Opportunity1.closeDate = date.today();
    Opportunity2.closeDate = date.today();
}

// In visualforce page

<apex:inputField value="{!Opportunity1.closeDate}" required="false">
<apex:inputField value="{!Opportunity2.closeDate}"  required="false">

// Just added required false so as to get rid of required validation.

 Let me know if you face any issues.

This was selected as the best answer
KawaiimomoKawaiimomo

Thanks a lot for your answer, Rahul.

 

Sadly this is not working for me because I am using a variable and not an object field. I got the error message 

Could not resolve the entity from <apex:inputField> value binding '{!startDate}'. <apex:inputField> can only be used with SObject fields 

However I figured out I can use Campaign.StartDate and EndDate as the inputField value, just need to be careful to not update the Campaign because I don't want to override the Date values.

Rahul SharmaRahul Sharma
Great to hear that worked.
But still I am eager to know, why the properties used with HTML tags are not able bind with Controller properties.
It would be great if someone could shed some lights on it.
Thanks
KawaiimomoKawaiimomo
Yes, it would be nice if someone could explain why is not working and if it
will be fixed. I've lost a lot of time trying to figure out why it wasn't
working :-/

Thanks again Rahul ;-)