Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"

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 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!

share|improve this question
Any reasons why you can not use Default Value Formula for your custom fields? – Sergey Utko Feb 21 '13 at 8:49
IMHO your Approach 1 in conjunction with 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. – bigassforce Feb 21 '13 at 9:27
up vote 1 down vote accepted

For the Approach2 i would take ApexPages.StandardController:

public with sharing class YourController{

    private MyObj__c obj;

    public YourController(ApexPages.StandardController controller){
        this.obj = (MyObj__c)controller.getRecord();

More info about StandardController Class

To Approach1.


It is not a problem to pass date parameter in a current user locale. For that just use format() method of the Date class:

String s =; 

and then:

'/a0H/e?DateField=' + s

The s looks for me in europe like 21.02.2013. In US it will be 02/21/2013.

From here

And for the default value of a custom field i would try to use a Default Value Formula

share|improve this answer
Thanks, just figured approach 2 out as well! Regarding Approach 1 - the thing is that the parameters are passed via string in a URL so you need /a0H/e?DateField=02/21/2013 for US users and /a0H/e?DateField=21.012013 for Europe. My experience has shown you can't mix and match. – Marc Feb 21 '13 at 9:28
@Marc It is not a problem to pass date parameter in a user locale: String s =; and then '/a0H/e?DateField=' + s – Sergey Utko Feb 21 '13 at 9:42
@Marc To rebuild the page use reRender parameter of any action element, like actionFunction or commantButton. – Sergey Utko Feb 21 '13 at 9:44
Could any one show me the save() code - do I have to make a mySave action and do upsert obj or can I wire obj to the standard save method? – Marc Feb 21 '13 at 10:00
Awesome, thanks @mast03 for Date.format() w/o params! – Marc Feb 21 '13 at 10:02

OK, I wanted to share some working code which illustrates Approach 2 and also shows how to intercept the save method:


    <apex:page standardController="Account" extensions="testAccountController">
      <apex:pagemessages /> 
      <apex:form >
            <apex:pageBlock mode="edit">
                <apex:pageBlockButtons > 
                    <apex:commandButton value="Save" action="{!save}"/>
                <apex:pageBlockSection title="Details" columns="1" >
                    <apex:inputField value="{!account.Name}" style="width:300px;"/>


    public class testAccountController {

        Apexpages.StandardController stdController;

        public testAccountController(ApexPages.StandardController stdController) {
            this.stdController = stdController;
            Account acct = (Account)stdController.getRecord();
            if ( == null) {
              // Set defaults for new records here
              // This is for fields that can still be changed in the page
              acct.CurrencyIsoCode = someFunction();

        public pageReference save() {
            // Get the secord being saved
            Account acct = (Account)stdController.getRecord();
            // Validate or make adjustments
            if ( == null) {
              // Set defaults for new records here
              // This is for fields that should not be changed in the page
            try {
            } catch (Exception e) {
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
            return null;

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.