[SalesForce] how to display default value in the vf page using apex;inputfield
Is it possible to display the default value of the pick list in object in visual Force page using apex:inputfield ?
Best Answer
In order to do that you need to make describe call and populate the selected value with it.
Lets assume you are holding value of selected country in selectedCountry varible then you need to call below method from constructor in order to show default picklist value for country picklist.
Code snippet:
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.
In your APEX controller code there will be a place where you fill the {!countriesOptions}
There you can use custom labels like you do on the visualforce page.
The first parameter is the value you use in code (so the untranslated value), the second parameter is the label shown to the end-user. So assuming you have a country named 'US' in the picklist, you'd need to create a Custom Label for it, add translations in the required language(s), and use like this:
That way the label will be shown on the VF Page to each user in their own language, but in the code you will always get the first parameter. E.g. selectedCountry2 will receive the value of the first parameter of the chosen country.
Best Answer
In order to do that you need to make describe call and populate the selected value with it.
Lets assume you are holding value of selected country in selectedCountry varible then you need to call below method from constructor in order to show default picklist value for country picklist. Code snippet:
Ref: https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_PicklistEntry.htm