+ Start a Discussion
HaroldHarold 

Select list not defaulting to value in field

Basically I have a numeric field in by object. A field that has existed for quite sometime.  There is also a visualforce page where the value is entered.  Recently we have decided that the only valid values for the field are 1 and 2.  So rather than changing the field type or tying validation rules to the field,  I've decided to change the visualforce page to just give them a select option of 1 or 2.  So I added the code below to my VF page.

 

<apex:column headervalue="Number of Motors" rendered="{!measureType='NVB'}" >
<apex:selectlist value="{!R.objTreatment.Installed_Count__c}" multiselect="false" size="1" >
<apex:selectOption itemValue="1" itemLabel="1" />
<apex:selectOption itemValue="2" itemLabel="2" />
</apex:selectlist>
</apex:column>

 

"R" is a wrapper, objTreatment is a custom object and of course installed Count is the field.  There is no code in the controller, it just loads the object from the database.  It works fine to set the field value on a save.  However it won't load with the correct value.  If I choose 2 and click save, it reloads the data and display's 1 even though the field is set to 2.  

 

I know I could probably create the select options list in the controller, then set the value.  But I'm hoping there's a way to make the select list default to the value in {!R.objectTreatment.Installed_Count__c} without moving the code to the controller.

HaroldHarold

I figured it out.  Duh!  Since the value in the database was actually 1.0 or 2.0 I needed to make the itemvalues match, So now it's

 

<apex:selectOption itemValue="1.0" itemLabel="1" />
<apex:selectOption itemValue="2.0" itemLabel="2" />