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 then ther is no problem we can do that by using .It is easy because  the field definition will define that,the page 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 looup 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’s get into the code.

 



 























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 getaccts() {
        List options = new List(); 
//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
    }
}

when this VisualForce page is rendered it display contact details and Account Name pop up menu where we can select to which account this contact is added insted of using look up.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!