Wednesday 28 November 2012

Salesforce: Custom Picklist in Visualforce Page

Suppose we are going to design a visualforce page and we add a picklist for users on the page. If the field where you want to store the selection is a picklist datatype then there is no problem we can do that by using <apex:selectList>.It is easy because  the field definition will define that ,the page will display a picklist when the Visualforce page is rendered.However, let's say that you want to display a picklist option based on a query from another object.
How would you do that ?.
It can be done by using look up field.we will make two object related by lookup relationship. When your user goes to use the lookup on the page, it opens a new window from which the user can search for records and then select one to populate back to the page.It will take more time to search . I was simply trying to limit the number and type of records that a relationship field made available to the user. I wanted to limit that functionality so that only specific records were permitted in the popup but still enforce the relationship integrity.
In this sample code I am going to make a Visualforce page to allow for editing of Contact records. However, I am going to create picklist options for the Account Name field instead of letting the user  to use Account Name the standard lookup functionality. So let us get into the code.

VisualForce Page
<apex:page StandardController="Contact" extensions="contactExtension"    standardStylesheets="true">

  <apex:sectionHeader title="Contact Edit" subtitle="{!Contact.Name}"    help="/help/doc/user_ed.jsp?loc=help">
   </apex:sectionHeader>
   <apex:form >
    <apex:pageBlock title="Contact Edit" mode="edit">
      <apex:pageBlockButtons >
         <apex:commandButton action="{!save}" value=" Save "></apex:commandButton>
         <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="General Information" columns="2">
        <apex:inputField value="{!Contact.FirstName}"></apex:inputField>
        <apex:inputField value="{!Contact.LastName}"></apex:inputField>
        <apex:inputField value="{!Contact.Department}"></apex:inputField>
        <apex:inputField value="{!Contact.Phone}"></apex:inputField>
        <apex:inputField value="{!Contact.Email}"></apex:inputField>
     </apex:pageBlockSection>
     <apex:pageBlockSection columns="1" showHeader="false">
       <apex:pageBlockSectionItem >
          <apex:outputLabel value="Account Name" for="accts"></apex:outputLabel>
          <apex:selectList id="accts" value="{!Contact.AccountId}" size="1" title="Account">
            <apex:selectOptions value="{!accts}"></apex:selectOptions>
          </apex:selectList>
       </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
    </apex:pageBlock>
 </apex:form>
</apex:page>
My Controller:-
public class contactExtension {
    private final Contact c; //User sobject
    
    /*initializes the private member variable u by using the getRecord method from the standard controller*/
    public contactExtension(ApexPages.StandardController stdController) {
        this.c = (Contact)stdController.getRecord();
    }
    
    //builds a picklist of account names based on their account id
    public List<selectOption> getaccts() {
        List<selectOption> options = new List<selectOption>(); 
    //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); 
    /*add the first option of '- None -' in case the user doesn't want to select a value or in       case no values are returned from query below*/
        for (Account account : [SELECT Id, Name FROM Account]) {  
   //query for Account records           
         options.add(new selectOption(account.id, account.Name));     
   //for all records found - add them to the picklist options       
      }       
      return options; //return the picklist options    }
}