We need to set some default values in apex on a custom object before it is created. The user should ideally work with the standard object page - we don't want to re-create the whole page in visualforce if not necessary.
Approach 1
Redirect the 'New' button to a custom VF page which loads the defaults and passes them on to the standard page via redirect using URL parameters.
Page (partial):
<apex:page standardcontroller="MyObj__c"
extensions="MyObjController"
action="!redirectDefaults">
Controller (partial):
public PageReference redirectDefaults() {
string defName = someComplexLogic();
PageReference retPage = new PageReference('/a0H/e?Name' + defName);
return retPage.setRedirect(true);
}
Question: The problem here is that fields must be passed in a locale specific format : you have to know the user locale and then pass mm/dd/yyyy or dd.mm.yyyy as the case may be. How can this be done?
Approach 2
Re-create the page in visual force as an extension of a standard controller. This is more work but I assume there must be a way to initialise an instance of myObj before the page is displayed.
Question: How do I initialise myObj in my controller before the form is loaded?
Any better approaches than these are more than welcome!
Default Value Formulafor your custom fields?PageReference.getParameters().putAll()is as good as it gets. Approach 2 will be very WET (opposite of DRY) and you don't get the URL parameterization of the input fields for free.