Boolean fields have three states, true, false, and null. It sounds like your picklist might be exhibiting the same issue.
Try prepopulating your object during the construction or action phase of the controller.
ie:
public Class MyController{
public object__c myObject__c {get;set;}
public MyController(){
myOjbect__c= new object__c(Question_1__c = 'Yes');
}
}
@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.
Best Answer
If you have any record types associated with the object, you'll need to select the default picklist value for those record types as well. That should solve your issue. If it doesn't, make sure your controller class is set to "with sharing" as that also may have an impact on the default value display.