You need to sign in to do that
Don't have an account?

VF Page - Select Contact from Account select list
Hi,
I have been reading through lots of posts, but I cannot figure out the solution to my problem.
I am trying to create a VF page from an account that lists all contacts associated with the account. The user should be able to select a contact and the contact is displayed.
I am able to display all the contacts, but not select one.
VF Page:
<apex:page controller="newOpportunityController2" tabStyle="Opportunity"> <apex:sectionHeader title="New Customer Quick Quote" subtitle="Step 1 of 3"/> <apex:form > <apex:pageBlock title="Customer Information" mode="edit"> <apex:pageBlockSection title="Account Information"> <!-- Within a pageBlockSection, inputFields always display with their corresponding output label. --> You are viewing the {!account.name} account. <p/> </apex:pageBlockSection> <apex:pageBlockSection title="Contacts"> <apex:selectList value="{!selectedContact}" multiselect="true"> <apex:selectOptions value="{!contact}"/> </apex:selectList> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel> <p>You have selected:</p> <apex:dataList value="{!selectedContact}" var="c">a:{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page>
My Controller:
public class newOpportunityController { Account account; Contact contact; public Account getAccount() { if(account == null) account = new Account(); return account; } public String selectedContact {get;set;} public List<SelectOption> contactList; public List<SelectOption> getContact () { if (contactList == null) { List<Contact> contactee = [select id, name, contact.accountid from Contact where contact.accountid = :ApexPages.currentPage().getParameters().get('id')]; contactList = new List<SelectOption>(); for (Contact c : contactee) { contactList.add(new SelectOption( c.id, c.name )); } } return contactList; } // String[] selectedContact = new String[]{}; public void setContact(String[] selectedContact) { this.selectedContact = selectedContact; } public String[] getSelectedContact() { return selectedContact; } }
I am pretty sure my problem is with my gets and set, but I can't figure it out and am getting really confused and frustrated!
Thanks in advance for any help.
David
There's one obvious issue - the controller that you have posted has a different name to the controller in the page, but I'm assuming that's a typo.
I've made some changes to your code (highlights in red) and its working (at least in my dev org) as I would expect.
Page
always a good idea to have pagemessages in there, as this will show you any errors that occur. Also, you weren't posting the changes back to the controller, so the selectedContact wouldn't have been updated.
Controller:
SelectedContact needs to be instantiated as an empty array, otherwise the page is trying to write your selections into a null property. I've also added the action method "test" which is invoked when you click the "Go" button.
All Answers
There's one obvious issue - the controller that you have posted has a different name to the controller in the page, but I'm assuming that's a typo.
I've made some changes to your code (highlights in red) and its working (at least in my dev org) as I would expect.
Page
always a good idea to have pagemessages in there, as this will show you any errors that occur. Also, you weren't posting the changes back to the controller, so the selectedContact wouldn't have been updated.
Controller:
SelectedContact needs to be instantiated as an empty array, otherwise the page is trying to write your selections into a null property. I've also added the action method "test" which is invoked when you click the "Go" button.
ok...that works,Thank you so much!
Now I have the id, but how do I reference elements from that contact?
I am sorry if this should be obvious, but I think my brain is fried.
David
The id is the value from the select list (which are value, name pairs).
As you have the ids, you can retrieve the contact details in the action method like so:
and then a slight tweak to your page:
you can then extract other fields in a similar way to the {!c.Name} snippet.
Thanks so much!
Could you expand this to become a full directory? Starting from selecting an account, then to an individual contact, but still having the account details displayed as well as the contact details you take from your queries?