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 ?
Best Answer
@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.You should note that
new Account()
andSObjectType.newSObject(Id, Boolean)
are very different. In order for you to get default values in a new SObject you must useSObjectType.newSObject(null, true)
. Try executing the following anonymous Apex to see the difference.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 inreset()
.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.