7

I have designed a VF page to create a new record in which I am trying to display the value of a pick-list field. I have set a default value for this pick-list field. When trying to create the record using the salesforce native page, the value with which pick-list loads is the one I set as default but when creating the same record using VF page, the pick-list loads with "--None--" as default value.

Can anyone suggest If this is salesforce related bug or Am I doing anything wrong ?

5
  • 1
    I'm assuming you are using an apex:inputField tag, yes? If so, what is the API version of your VF page? According to the documentation: "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."
    – JCD
    Commented Sep 19, 2012 at 13:48
  • Yes you are correct. I am using apex:inputField tag. API Version of my VF page is 24.0. Commented Sep 19, 2012 at 13:54
  • 1
    Are you using the standard controller for your object, or a custom Apex controller? Commented Sep 19, 2012 at 16:11
  • I am using Standard controller.. Commented Sep 20, 2012 at 16:52
  • I'm using a custom controller with API version 31. Also using apex:inputfield and it's not loading the value I've selected as the default value declaritively on my vf page. It displays --None-- just like the behavior you've described.
    – Jay Bolt
    Commented Jul 8, 2015 at 18:57

4 Answers 4

3

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.

1
  • 1
    There is no record type associated with object and also controller class is set to "with sharing" mode. Commented Sep 20, 2012 at 16:59
2

As of Winter '14, if this was a bug it appears to have been fixed. The following code displays the default value for the picklist field.

<apex:page standardController="Custom_Object__c">
  <apex:form>
    <apex:inputField value="{!Custom_Object__c.Picklist__c}"/>
  </apex:form>
</apex:page>
0

Maybe this is an option to work around your problems: Use a link or button to call your Visualforce page and add the field id and the default value to the page's URL as described here: http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

e. g. /visualforcepageurl?fieldId=DefaultValue

1
  • This only works with standard edit pages, I believe. Shouldn't work with Visualforce. Commented Aug 26, 2014 at 22:51
0
public class sam {
    public string str {get; set;}
    public List<SelectOption> lstPicks {get; set;}
    
    public sampleCon() {
        str = 'Two';
    }
    public List<SelectOption> getPicks() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('One','One'));
        options.add(new SelectOption('Two','Two'));
        options.add(new SelectOption('Three','Three'));

        return options;
    }   
}

<apex:page controller="Sam"> 
  <apex:form> 
    <apex:selectList value="{!str}" size="0"> 
      <apex:selectOptions value="{!Picks}"/> 
    </apex:selectList> 
  </apex:form> 
</apex:page>
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .