3

This is one of the feature that was delivered "Winter 11" but looks like there are some limitation. The suggested feature seems to work only for the very first time when VF page loads but when a rerender is done and the underlying object is re-initialised it doesn't seem to pull the default value.

Code to replicate the same

    <apex:page docType="html-5.0" standardcontroller="Account" extensions="TestPage_Con">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="reset" action="{!reset}"/>
            <apex:pageBlockSection >
                <apex:inputField value="{!myAccount.MyField_With_Default_value__C}"  required="false"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


public class TestPage_Con {
    public Account myAccount{get;set;}

    public TestPage_Con(ApexPages.StandardController controller) {
        myAccount = new Account();
    }   

    public void reset(){
        myAccount = new Account();
    }
}

Assumption

  • MyField_With_Default_value__C is a field with a default value defined in field definition (Not controller)

Observation : The value loads for the first time but if you press the reset button the field clears out.

I am trying to avoid any value being assigned from controller or using field describe rather want to use the feature delivered in winter 11. Is am doing anything wrong here ?

2

@Avidev9, I think you've stumbled upon a bug in the system, although it's slightly different from what you originally described. But first, would you try the following workaround? It should allow you to use partial page refreshes (i.e., rerender) in Visualforce.

public class TestPage_Con {
    public Account myAccount{get;set;}

    /*
     * A template from which new accounts will be instantiated
     */
    private Account accountTemplate;

    public TestPage_Con(ApexPages.StandardController controller) {
        //myAccount = new Account();
        accountTemplate = (Account)Account.SObjectType.newSObject(null, true);
        myAccount = accountTemplate.clone(false, true, false, false);
    }   

    public void reset(){
        //myAccount = new Account();
        //myAccount = (Account)Account.SObjectType.newSObject(null, true);
        myAccount = accountTemplate.clone(false, true, false, false);
    }
}

You should note that new Account() and SObjectType.newSObject(Id, Boolean) are very different. In order for you to get default values in a new SObject you must use SObjectType.newSObject(null, true). Try executing the following anonymous Apex to see the difference.

System.debug((Account)Account.sObjectType.newSObject(null, true));
System.debug(new Account());

With that said, I do think there is still a bug. Notice that the code I shared shows an Account "template" being created and kept, because for some reason SObject.newSObject() breaks down when executed through a Visualforce action. Notice in the code sample above the second commented-out line in reset().

I strongly encourage you log a case with Salesforce Support and pursue this as a bug, so that you're able to confidently leverage what should be a GA feature.

| improve this answer | |
2

Per the docs default values are set for input fields

Beginning with API version 20.0, an inputField matched to a field with a default value has the default value prepopulated on the Visualforce page.

However, there is a known issue that a field's default value is not sent back to a controller in the postback. So if you do a re-render the value is lost. If this bug affects you please visit the issue and click the "This Issue Affects Me" button.

In your case you can reset the field back to it's default directly using a field describe, i.e.

public void reset {
  Custom_Object__c obj = (Custom_Object__c) controller.getRecord();
  if(obj.Picklist__c == null) {
    obj.Picklist__c = (String) 
      Custom_Object__c.Picklist__c.getDescribe().getDefaultValue();
  }
}
| improve this answer | |
1

Ok.

I see what is happening. Although not ideal this will solve your issue

public pagereference reset(){
        Pagereference pr = New Pagereference('/apex/vfpagetest');
        pr.setRedirect(true);
        return pr;
    }

replace the vfpagetest with the vf page your are using. Or you can construct the url as needed.

| improve this answer | |
0

From what I understood, default values are not assigned by default when creating object from APEX. If you went and did something like the following, the default value would have not been set.

Account myAccount = new Account();
System.debug('** before insert ' + myAccount.defaultValueField__c);
insert myAccount;
System.debug('** after insert ' + myAccount.defaultValueField__c);

This will output the following:

** before insert null
** after insert null

Here's a workaround:

public PageReference reset(){
        PageReference page = ApexPages.currentPage();
        page.setRedirect(true);
        return page;
}

Another solution would be the following, only if your default value is not a formula.

public void reset(){
    myAccount = new Account();
    myAccount.defaultValueField__c = Account.defaultValueField__c.getDescribe().getDefaultValueFormula();
}

If your value is not a string then you will need to cast it as the following code will return a string value. E.g.

myAccount.defaultValueField__c =  double.valueOf(Account.defaultValueField__c.getDescribe().getDefaultValueFormula());
| improve this answer | |
  • You are correct BUT, when rendered in VF with input field the default value is populated with the default value. During an AJAX request the value is sent to the controller and stored appropriately. – Eric Jan 3 '14 at 21:13
  • Yes, I should have mentioned that to avoid any confusion and also made a link as why it's initially set when the page is loaded and not after his reset call. Thanks for the precision. – ChopaForce Jan 3 '14 at 21:18
  • Correct. I also noticed that the reset is creating a new account which is where your comment comes into play. I overlooked that initially – Eric Jan 3 '14 at 21:22
  • Yes forgot to remove it. I edited my answer to reflect it. – ChopaForce Jan 3 '14 at 21:29
  • well I fear I can't use this one as I will loose all other variables in the controller. Ideally I should be using ajax/rerender, the above code is a strip down version of actual code. – Avidev9 Jan 3 '14 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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