+ Start a Discussion
LloydCLloydC 

Custom picklist or lookup

Hi,

 I am trying to create a wizard that will create a case based on user input to the pages. 

On the first page the user selects the account from a lookup.  On the second screen, I only want to allow the user to pick a contact that is associated with the account selected on the first screen.  I implemented a lookup but it shows all contacts.  Is there a way to show only the contacts on the account selected; or, if not, how can I implement a select list with only the associated contacts?

 

Thanks,

Lloyd

Best Answer chosen by Admin (Salesforce Developers) 
Greg HGreg H

If you wanted to use a picklist in place of the standard lookup field you could include the apex:pageBlockSectionItem tag in your visualforce page. It may look something like this:

<apex:pageBlockSectionItem> <apex:outputLabel value="Contacts" for="c"></apex:outputLabel> <apex:selectList id="c" value="{!Custom_Field}" size="1" title="Contact"> <apex:selectOptions value="{!contactlist}"></apex:selectOptions> </apex:selectList> </apex:pageBlockSectionItem>

 

The "{!Custom_Field}" piece is the field where the selection is stored. Then you'll need to have add something like the following to your extension or custom controller:

 

public List<selectOption> getContactlist() { 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 (Contact contacts : [SELECT Id, Name FROM Contact WHERE AccountId = 'XXXXXXXXXXXXXXXXXX']) { //query for Contact recordsassociated to your account Id options.add(new selectOption(contacts.Id, contacts.Name)); //for all records found - add them to the picklist options } return options; //return the picklist options }

 

You will need to adjust the query portion of the code above to suite your specific needs but this should at least get you pointed in the correct direction.

-greg

All Answers

Greg HGreg H

If you wanted to use a picklist in place of the standard lookup field you could include the apex:pageBlockSectionItem tag in your visualforce page. It may look something like this:

<apex:pageBlockSectionItem> <apex:outputLabel value="Contacts" for="c"></apex:outputLabel> <apex:selectList id="c" value="{!Custom_Field}" size="1" title="Contact"> <apex:selectOptions value="{!contactlist}"></apex:selectOptions> </apex:selectList> </apex:pageBlockSectionItem>

 

The "{!Custom_Field}" piece is the field where the selection is stored. Then you'll need to have add something like the following to your extension or custom controller:

 

public List<selectOption> getContactlist() { 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 (Contact contacts : [SELECT Id, Name FROM Contact WHERE AccountId = 'XXXXXXXXXXXXXXXXXX']) { //query for Contact recordsassociated to your account Id options.add(new selectOption(contacts.Id, contacts.Name)); //for all records found - add them to the picklist options } return options; //return the picklist options }

 

You will need to adjust the query portion of the code above to suite your specific needs but this should at least get you pointed in the correct direction.

-greg

This was selected as the best answer
LloydCLloydC

Thank you Greg.  I was working in the right direction but couldn't quite get it.

Thanks again.

Lloyd