+ Start a Discussion
lauraecilauraeci 

Storing an option from apex:selectoption in a custom field on a custom object

I have a simple VisualForce page which allows me to select 3 different options in a dropdown.  I'd like to be able to store that result in a field on a custom object but I'm not sure how to do that.  I can't use a picklist type.  

 

Thanks!

 

<apex:page >
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page: OptionPicker
<!-- End Default Content REMOVE THIS -->
<apex:form >
<apex:selectList id="recent" size="1" >
<apex:actionSupport event="onchange" action="{!changeRt}" reRender="recentmodify"></apex:actionSupport>
<apex:selectoption itemLabel="option1" itemValue="card"></apex:selectoption>
<apex:selectoption itemLabel="option2" itemValue="check"></apex:selectoption>
<apex:selectoption itemLabel="option3" itemValue="Scholarship"></apex:selectoption>
</apex:selectList>
</apex:form>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
lauraecilauraeci

Thanks, I found a way to do it with the standardController...

 

<apex:pagestandardController="MyOptions__c">

  <apex:form>

  <apex:selectListid="recent"size="1"value="{!MyOptions__c.Option__c}">

            <apex:selectoptionitemLabel="option1"itemValue="card"></apex:selectoption>

            <apex:selectoptionitemLabel="option2"itemValue="check"></apex:selectoption>

            <apex:selectoptionitemLabel="option3"itemValue="Scholarship"></apex:selectoption>

     </apex:selectList>  

     <apex:commandButtonvalue="Save"action="{!save}"/></p>

  </apex:form>

</apex:page>

All Answers

Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)

Yeah you can do that.

 Create one Apex class and use this class as controller with visualforce page and create new buttton that will call to Save method and save the record.

 

public Class ClassName{

  public string str{get;set;}

  public pagereference Save(){

   //Here set the str value with object`s field.

   //like you  want to create new account with selected value.

   Account act = new Account(name=str);

  insert act;

  return null;

 }

}

 

// your VF Page:

<apex:page  controller="ClassName">
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page: OptionPicker
<!-- End Default Content REMOVE THIS -->
<apex:form >
<apex:selectList id="recent" size="1" >
<apex:actionSupport event="onchange" action="{!changeRt}" reRender="recentmodify"></apex:actionSupport>
<apex:selectoption itemLabel="option1" itemValue="card"></apex:selectoption>
<apex:selectoption itemLabel="option2" itemValue="check"></apex:selectoption>
<apex:selectoption itemLabel="option3" itemValue="Scholarship"></apex:selectoption>
</apex:selectList>

 <apex:commandbutton value="Save selected value!" action="{!Save}"/>
</apex:form>
</apex:page>

lauraecilauraeci

Thanks, I found a way to do it with the standardController...

 

<apex:pagestandardController="MyOptions__c">

  <apex:form>

  <apex:selectListid="recent"size="1"value="{!MyOptions__c.Option__c}">

            <apex:selectoptionitemLabel="option1"itemValue="card"></apex:selectoption>

            <apex:selectoptionitemLabel="option2"itemValue="check"></apex:selectoption>

            <apex:selectoptionitemLabel="option3"itemValue="Scholarship"></apex:selectoption>

     </apex:selectList>  

     <apex:commandButtonvalue="Save"action="{!save}"/></p>

  </apex:form>

</apex:page>

This was selected as the best answer