0

Can anyone please help me understand how to save a selected value from a picklist, I mean I don't understand how to save the value in a record, along with other fields in a custom object. Here is the sample code that I need help with. Thanks.

    <apex:page standardController="customobject__c" extensions="ext">

<apex:form >
 <apex:pageBlock >
 <apex:pageBlockSection >
  <apex:pageBlockSectionItem >
         <apex:outputLabel value="Select option" />
         <apex:selectList size="1"  value="{!selectedReason}" style="width:310px;" multiselect="false" >
         <apex:selectOptions value="{!Reasons}"  />
         </apex:selectList>
   </apex:pageBlockSectionItem>
   </apex:pageBlockSection>  
   </apex:pageBlock>
            <apex:commandButton value="submit" action="{!submitSave}"/>

   </apex:form>    

</apex:page>

The Class

public class ext {
      public customobject__c cb{get;set;}

public String selectedReason{get;set;}
    public ext(ApexPages.StandardController controller) {
c= new customobject__c();
    }
public void submitSave()
        {
         }        
public list<selectoption> getReasons()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('--None--','--None--'));
      options.add(new SelectOption('One','One'));
      options.add(new SelectOption('Two','Two'));

   return options;
 }  
}

1 Answer 1

1

rest of the code looks fine, just change folling method

public void submitSave(){
//at this point of execution - variable 'selectedReason' has been updated with value selected on page


// just assign this value to a given custom object & ou are good to go.

customobject__c  co = new customobject__c ();

co.selectedReason__C = selectedReason;

insert co;
}
2
  • do I need to create a custom field selectedReason__c and bind selectedReason to it?, if yes what type should selectedReason__c be? Thanks.
    – cartman
    Jan 30, 2014 at 4:56
  • If such field exits on object, use the same. Else use field type as 'Text'. Jan 30, 2014 at 5:26

You must log in to answer this question.

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