I have a visualforce page to generate a OpportunityLineItem. It is launched from the Opportunity page. I want a picklist for users to enter a custom field Product_Type__c.

The Opportunity is the controller of record on the page.

I have been able to query the schema for the values. How do I assign this to field?

Here is my Controller snippet:

public class AddProduct {

    private ApexPages.StandardController sc;
    public Opportunity record {get; private set;}
    public OpportunityLineItem ELI {get; set;}
    public OpportunityLineItem CusLi {get;set;}
    public Opportunity Op {get; set;}

    public List<SelectOption> getProdSubTypes1(){
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = OpportunityLineItem.Product_SubType__c.getDescribe(); 
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry f : ple)
          options.add(new SelectOption(f.getLabel(), f.getValue()));
       return options; 

    public AddProduct(ApexPages.StandardController controller) {
        if (!Test.isRunningTest()) Controller.addFields(new List<String>{'Name', 'PriceBook2Id'});
        record = (Opportunity)controller.getRecord();
        sc= controller;
        ELI = new OpportunityLineItem();
        CusLI = new OpportunityLineItem();

 public PageReference AddEmailProduct (){

        Opportunity TheOpp = (Opportunity)sc.GetRecord();
        ELI.PricebookEntryId=[select id from PricebookEntry where name =: 'Email' 
                            and Pricebook2id=:Theopp.Pricebook2Id].id;

    ELI.Product_SubType__c = ????

        insert ELI;

VF Page Snip:

<apex:commandButton action="{!AddEmailProduct}" value="Email" />           
<apex:selectList size="1">
<apex:selectOptions value="{!ProdSubTypes1}"></apex:selectOptions>

2 Answers 2


You need to bind the value attribute of apex:selectList as below

<apex:selectList value="{ELI.Product_SubType__c}" size="1">
    <apex:selectOptions value="{!ProdSubTypes1}"></apex:selectOptions>

For more examples on usage refer : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm

  • Do I need to modify the class part ELI.Quantity=1; ELI.Product_Type__c='Email'; ELI.Product_SubType__c = ???? b/c it is not pushing the value
    – jaw999
    Aug 22, 2017 at 18:47
  • can i have it set as a param ?
    – jaw999
    Aug 22, 2017 at 18:48
  • You can remove the line ELI.Product_SubType__c = ???? because its value will be sent automatically when you invoke the action. Aug 22, 2017 at 22:18

This works with a change to the suggested answer. I still query for the picklist values but I added a variable to the class :

public String SubType{get;set;} 

Then on the visualforce page I make the variable the selectlist value:

 <apex:selectList value="{!SubType}" size="1">
    <apex:selectOptions value="{!SubTypeList}">

This passes the value correctly.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.