+ Start a Discussion
motelymotely 

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

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

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

 

 

<apex:page controller="newOpportunityController" tabStyle="Opportunity">
  <apex:pageMessages id="msgs"/>
  <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:commandButton value="Go" action="{!test}" rerender="out, msgs" status="status"/>
  </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>

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:

 

 

public class newOpportunityController {

   Account account;
   Contact contact;

   public Account getAccount() {
        if(account == null) account = new Account();
      return account;

   } 
    public String[] selectedContact=new String[]{};
    
    public String[] getSelectedContact()
    {
    	return selectedContact;
    }
    
    public void setSelectedContact(String[] sel)
    {
    	selectedContact=sel;
    }
    
    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;
    }
 
   public PageReference test()
   {
   	return null;
   }

}

 

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

bob_buzzardbob_buzzard

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

 

 

<apex:page controller="newOpportunityController" tabStyle="Opportunity">
  <apex:pageMessages id="msgs"/>
  <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:commandButton value="Go" action="{!test}" rerender="out, msgs" status="status"/>
  </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>

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:

 

 

public class newOpportunityController {

   Account account;
   Contact contact;

   public Account getAccount() {
        if(account == null) account = new Account();
      return account;

   } 
    public String[] selectedContact=new String[]{};
    
    public String[] getSelectedContact()
    {
    	return selectedContact;
    }
    
    public void setSelectedContact(String[] sel)
    {
    	selectedContact=sel;
    }
    
    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;
    }
 
   public PageReference test()
   {
   	return null;
   }

}

 

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.

 

 

 

This was selected as the best answer
dmotedmote

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

bob_buzzardbob_buzzard

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:

 

 

   public List<Contact> contacts {get;set;}

   public PageReference test()
   {
   	// blank out any earlier contact details
   	contacts=new List<Contact>();
   	if (null!=selectedContact)
   	{
   	   contacts=[select id, name from Contact where id IN :selectedContact];
   	}
   	return null;
   }

 

 

and then a slight tweak to your page:

 

 

   <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel >
                    <p>You have selected:</p>
                    <apex:dataList value="{!contacts}" var="c">a:{!c.Name}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>

 

you can then extract other fields in a similar way to the {!c.Name} snippet.

 

 

 

 

 

dmotedmote

Thanks so much!

b-liub-liu

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?