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
TeamFuguTeamFugu 

How do I change the default date for the DatePicker?

We have some general script that is loaded for every page that adds the JQueryPickDate calendar wherever it sees a date field. What I need to do is find a way to change the default date from today to some other data based on an object behind the form. Since the script is not directly in my VisualForce page, how do I find the date picker and change the default date?

 

At first, I tried to add code to the onfocus event for the input box but the script overrides this and does not allow me to handle the event. The problem is that have a form that shows multiple records with an ID, a date, and a comment. I can populate all of the dates where the ID is blank but that does not look good to have records with dates that do not have an ID.

 

Is there any way that I can do this from the controller?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
The date picker is a standard widget. If you want to use another (such as jQuery), use an apex:inputText element instead of apex:inputField. This would give you more control over the date picker.

It's possible to "hack" the date picker provided by salesforce.com, but I would advise against that for practical reasons (if they change their library, it could break your page).

If you stick with an apex:inputField, though, you can not only default the value for the field (which the date picker should support), you can also add a validation rule that will cause an error if the date is too early; using the standard save controller will suffice for that purpose.

Working with the standard UI instead of trying to get really custom in your first week is probably a better idea. There's a lot you can do with just CSS to dress up the page. Using a standard inputField will also automatically support internationalization, field name changes, etc.

All Answers

sfdcfoxsfdcfox
You may set the default date via a Default Value Formula on the field, or by setting it in the extension's single argument constructor (via the ApexPages.StandardController).
TeamFuguTeamFugu

The problem is that I am not explicitly using the date picker in my page definition. It is being added when the page is being built, I believe by the style sheets, so I don't have easy access to it from the page definition.

 

On a side note, I tried adding a method to the standard controller and using it in the feild I want but when I try to deploy the page, it says it cannot find the method and will not deploy to my sandbox. If the controller is defined in the apex:page section, does it go out of scope in the apex:pageBlock sections?

sfdcfoxsfdcfox

Controllers are in scope for the entire page in which they are declared for (inner scopes from included pages and components have their own controllers and/or extensions).

 

Your page code should look like this:

 

<apex:page standardController="Object_Name__c" extensions="ObjectNameExtension">
 <!-- your code here -->
</apex:page>
public with sharing class ObjectNameExtension {
  public ObjectNameExtension(ApexPages.StandardController controller) {
    Object_Name__c record = (Object_Name__c)controller.getRecord();
    // For first day of the month, for example.
    record.Date_Field_To_Default__c = Date.Today().toStartOfmonth();
  }
}

You can't call methods directly except as action methods, but there's no reason to do so. Your code's constructor will set the correct date for you. You could even run a query in the constructor to determine the date you'd like to use.

 

 

 

TeamFuguTeamFugu

Part of my problem is that this is the first full week using SalesForce. My prior experience with web services had been with JavaServlets creating web pages by brute force. The issue I have is that I have a feature where I'm linking one activity to contacts with a "go live date". The form allows the user to add contacts to the activity and the date is tied to a date picker. The date picker is being applied when the page is rendered by SalesForce and not from my page definition. The default date is the current date and I'd like to make sure that the date is set to the default date in the activity rather than the current date.

 

I'm also trying to validate the selected date on the onblur event to ensure that the client's go live date is not before the activity's go live date. When I do this, I get an error on the page stating that an object is expected with the name of the standard controller class name. I have JavaScript just inside the <apex:page> tag and it doesn't seem to be called. In the field's tag, I have an attribute for onblur="myFunction( this )" to call my script function.

 

I'm having a hard time figuring out how much is changed by SalesForce when the page is generated and how much I can do as standard HTML and JavaScript.

TeamFuguTeamFugu

I have added code in the submit buttons controller to force the date to be that of the activity if it is after the selected date but I think this might confuse the user when they submit the form and the date changes on them. I'd like to handle this in the page script so that I could alert them that the date must be that of the activity or later.

sfdcfoxsfdcfox
The date picker is a standard widget. If you want to use another (such as jQuery), use an apex:inputText element instead of apex:inputField. This would give you more control over the date picker.

It's possible to "hack" the date picker provided by salesforce.com, but I would advise against that for practical reasons (if they change their library, it could break your page).

If you stick with an apex:inputField, though, you can not only default the value for the field (which the date picker should support), you can also add a validation rule that will cause an error if the date is too early; using the standard save controller will suffice for that purpose.

Working with the standard UI instead of trying to get really custom in your first week is probably a better idea. There's a lot you can do with just CSS to dress up the page. Using a standard inputField will also automatically support internationalization, field name changes, etc.
This was selected as the best answer
TeamFuguTeamFugu

The page displays the current records plus places for 10 new records at a time. I can preset the date fields and the date picker does honor it but it looks odd to have a bunch of pre populated dates with no ID associated with it.

 

I did add code to the controller to force the date to be that of the activity if the submitted date is prior to the activity's date. I don't show any errors but just adjust the date.

 

I sure would love to find an easy way to set the date after a new ID has been entered but the names of all of the form fields are generated on the fly and I'm not ready to try to get that cute yet.