function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Giancarlo AmatiGiancarlo Amati 

how to save the value from a picklist in a VF

Hi Team,

I have a picklist field from the Contact object in my VF page. The code is the one below. However, when I select a value for the picklist and the I continue to complete the form, the Contact records gets refreshed, but the value that I selected on the VF page is not saved automatically in the field. 

Am I missing something? does it have to go through the controller? 
Thank you,

GC
User-added image

<apex:pageBlockSectionItem rendered="{!NOT(confirmMode)}" >
                    <apex:outputLabel value="LeadSource" />
                    <apex:outputPanel layout="block" styleClass="requiredInput">
                        <apex:outputPanel layout="block" styleClass="requiredBlock"/>
                        <apex:inputfield value="{!contactObj.LeadSource}"/>
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>
                               
                
                <apex:pageBlockSectionItem rendered="{!NOT(confirmMode)}" />

                <apex:pageBlockSectionItem rendered="{!(confirmMode)}" >
                    <apex:outputLabel value="LeadSource" />
                    <apex:outputtext value="{!contactObj.LeadSource}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem rendered="{!(confirmMode)}" />

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Giancarlo Amati,

You can get Reference from this code, maybe it will help you:

///////////Apex Code:
public class dynamicpicklist
{
public String city{get; set;}

public List<SelectOption> getcitynames()
{
  List<SelectOption> options = new List<SelectOption>();
  List<DynamicPicklist__c> citylist = new List<DynamicPicklist__c>();
  citylist = [Select Id, PicklistValue__c FROM DynamicPicklist__c ];
  options.add(new SelectOption('--None--','--None--'));
  for (Integer j=0;j<citylist.size();j++)
  {
      options.add(new SelectOption(citylist[j].PicklistValue__c,citylist[j].PicklistValue__c));
  }
  return options;
}
public String newpicklistvalue{get; set;}

public void saverec()
{
  DynamicPicklist__c newrec = new DynamicPicklist__c(PicklistValue__c=newpicklistvalue);
  insert newrec;
  newpicklistvalue=NULL;
}

}

//////////Visualforce Code:
<apex:page controller="dynamicpicklist" sidebar="false" >
<apex:form >
<apex:sectionHeader title="Dynamic Picklist" subtitle="Reusable code"/>
<apex:pageblock >
<apex:pageBlockSection title="Dynamic picklist" columns="1">
      <apex:pageblocksectionItem >
          <apex:outputlabel value="City" for="values" />
          <apex:selectList value="{!city}" size="1" id="values">
              <apex:actionSupport event="onchange" reRender="newvalue" />
              <apex:selectOptions value="{!citynames}"/>
          </apex:selectList>
      </apex:pageblocksectionItem>                                        
          <apex:outputpanel id="newvalue">
             <apex:outputpanel rendered="{!city == '--Other--'}">
             <div style="position:relative;left:75px;">             
                  <apex:outputlabel value="New value" for="newval" />
                  <apex:inputText value="{!newpicklistvalue}" id="newval"/>
                  <apex:commandbutton action="{!saverec}" value="Add!"/>
             </div>
             </apex:outputpanel>
          </apex:outputpanel>             
  </apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

if you have any query please comment.
---------------
If you find your Solution then mark this as the best answer to close this question. 

Thank you!
Regards,
Suraj Tripathi

--