Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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!

share|improve this question
    
Any reasons why you can not use Default Value Formula for your custom fields? –  mast0r Feb 21 '13 at 8:49
1  
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. –  user320 Feb 21 '13 at 9:27

2 Answers 2

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.

UPDATE:

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 = Date.today().format(); 

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
1  
@Marc It is not a problem to pass date parameter in a user locale: String s = Date.today().format(); and then '/a0H/e?DateField=' + s –  mast0r Feb 21 '13 at 9:42
    
@Marc To rebuild the page use reRender parameter of any action element, like actionFunction or commantButton. –  mast0r 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:

Page:

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

Controller:

    public class testAccountController {

        Apexpages.StandardController stdController;

        public testAccountController(ApexPages.StandardController stdController) {
            this.stdController = stdController;
            Account acct = (Account)stdController.getRecord();
            if (acct.id == 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 (acct.id == null) {
              // Set defaults for new records here
              // This is for fields that should not be changed in the page
            }
            try {
              return stdController.save();
            } catch (Exception e) {
              ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
            }
            return null;
        }

    }
share|improve this answer

Your Answer

 
discard

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.