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
Raymond AireyRaymond Airey 

Adding Customer Picklist to Visualforce Page

Hi All,

I am very new to Apex coding and I am trying to learn how to add a custom picklist field to a Visualforce page.

I am trying to work with the below:
  • A customer field called "Type_of_Sales__c" within the standard OpportunityLineItem object
  • The custom field contains the following items
    • Sample
    • Normal Sales
    • Inspection Copy
    • Schools Direct
Using the below code, I'm able to display the Heading but not the list:
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Type_of_Sales__c.Label}">
</apex:column>
I have tried using the following code, which again shows the heading but no list:
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Type_of_Sales__C.Label}">
<apex:inputField value="{!OpportunityLineItem.Fields.Type_of_Sales__C}"
</apex:column>
All I'm am trying to do is display to customer picklist as you would normally see it on a page.

Any help would be great!
 
LBKLBK
Hi Raymond,

Here is a sample code for you to refer.
<!--VF Page-->
<apex:page controller="clsContact" >
    <apex:form >
        First Name : <apex:inputField value="{!contact.FirstName}"/>
        Last Name: <apex:inputField value="{!contact.LastName}"/>
        Email: <apex:inputField value="{!contact.Email}"/>
        Lead Source : <apex:inputField value="{!contact.LeadSource}"/>  
    </apex:form>
</apex:page>

//APEX Controller
public class clsContact {
    public Contact contact {get; set;}
    String contactId {get; set;} 
    
    public clsContact(){
        contactId = ApexPages.currentPage().getParameters().get('id');
        if(contactId != null){
            getContact();
        }
        else{
            contact = new Contact();
        }
    }
    public void getContact() {
        
            contact = [SELECT Id, FirstName, LastName, Email, LeadSource FROM Contact WHERE Id = :contactId];
    }

}

Your input field is supposed to be like this, if you are using the StandardController.
<apex:inputField value="{!OpportunityLineItem.Type_of_Sales__C}"
Let me know if this helps.
 
Raymond AireyRaymond Airey
Hi LBK

Thanks for your reply!

I have changed my code to look like the below:
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Type_of_Sales__c.Label}">
<apex:inputField value="{!OpportunityLineItem.Type_of_Sales__C}"/>
</apex:column>
But I get the following error:
Error	Error: Unknown property 'OpportunityStandardController.OpportunityLineItem'
Any ideas what the issue might be?
 
Raymond AireyRaymond Airey
I've been playing around have come across what I thought was a fix but is another issue.

I have now created the following code:
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Type_of_Sales__c.Label}">
<apex:inputField value="{!s.Type_of_Sales__c}" required="True"/>
</apex:column>
Which works perfectly but if I save my 'Product' selection's and then re-enter the page, I get the following error:
SObject row was retrieved via SOQL without querying the requested field: OpportunityLineItem.Type_of_Sales__c

But if I change my code to the following:
<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Type_of_Sales__c.Label}">
<apex:inputField value="{!s.Type_of_Sales__c}" required="True" render="True"/>
</apex:column>
The code works without the error but I'm also missing my dropdown box.

Any pointers would be great?
LBKLBK
Can you post your code here? It would be a lot easier if I get to see the code in complete context.