function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
b-liub-liu 

Account SelectList, Contact Select List, and Displaying details

Hey guys,

 

Coming across a post I found the select list to select contacts from an account. I've been trying to make it one level up so that you can select the available accounts and display their details. Then be able to select the cooresponding contacts after to see their details.

 

VF page looks like this:

<apex:page controller="newController" tabStyle="Account">
  <apex:pageMessages id="msgs"/>
  <apex:sectionHeader title="Getting this thing to finally work" />
    <apex:form >
      <apex:pageBlock title="Contact Directory" mode="edit">
      <apex:pageBlockSection title="Account Information">
        <apex:selectList value="{!selectedAccount}" multiselect="true" style="height:400px; overflow:auto;">
            <apex:selectOptions value="{!account}"/>
         </apex:selectList><p/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contacts">
         <apex:selectList value="{!selectedContact}" multiselect="true" style="height:400px; overflow:auto;">
            <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="sc">
                        <h3>Account ID: {!sc}</h3>
                    </apex:dataList>
                    <apex:dataList value="{!contacts}" var="c">
                        Name: {!c.name} Phone: {!c.phone} Email: {!c.email}
                    </apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
    
</apex:page>

 

 

Here's my controller:

public class newController {

    Account account;
    Contact contact;

    
    public String[] selectedAccount=new String[]{};
    
    public String[] getSelectedAccount()
    {
        return selectedAccount;
    }
    
    public void setSelectedAccount(String[] sel) {
        selectedAccount=sel;
    }
    
    public List<SelectOption> accountList;
    
    public List<SelectOption> getAccount () {
        if (accountList == null) {
            List<Account> accountee = [select id, name, phone, website from Account limit 100];
          
            accountList = new List<SelectOption>();
            
            for (Account a : accountee) {
                accountList.add(new SelectOption( a.id, a.name ));
            }
        }
        return accountList;
    }
    

    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')
                                        limit 100];
          
            contactList = new List<SelectOption>();
            
            for (Contact c : contactee) {
                contactList.add(new SelectOption( c.id, c.name ));
            }
        }
        return contactList;
    }
 
    public List<Contact> contacts {get;set;}
    
    public PageReference test(){
        // blank out any earlier details
        
        account=new List<Account>();
        if (null!=selectedContact){
            account=[select id, name, phone, website from Account where id IN :selectedAccount];
        }
        
        contacts=new List<Contact>();
        if (null!=selectedContact){
            contacts=[select id, name, phone, email from Contact where id IN :selectedContact];
        }
        
        return null;
    }  
}

 I'm stuck on what to do and the controller is giving me the hardest time.

 

Thanks