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
dmotedmote 

Help on VF Select List signle value

I am trying to create a VF page where I show all contacts related to an account.  Once the contacted is selected and the user presses go, I want to display the name of the contact.  I can get it to display the id, but I can't figure out how to access the other contact fields.  Does this have to do with returning the contact id as a string?  Can I return the id as a contact?  I have been trying to do this for a while, and I can't figure it out.  I have also read through a ton of docs, but can only make it work when multiselect = true.  I want to do this when multiselect = false. 

 

Here is my code. 

 

Thanks for your help.

 

Apex 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="false">
            <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">{!c.name}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
    
</apex:page>

 

Controller:

 

public class newOpportunityController {

    public String getContacts() {
        return null;
    }


   Account account;
   Contact contact;

   public Account getAccount() {
        return [select id, Name, Phone from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public String selectedContact;
    
    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;
    }
 
    Contact contacts;
//    public Contact contacts() {get;set;}

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


}

 

 

 

Alok_NagarroAlok_Nagarro

Hi,

 

Try this code...

 

<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="false">
            <apex:selectOptions value="{!contactList}"/>
         </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="{!contacts}" var="c">
                    <apex:outputText> {!c.firstname}</apex:outputText>
                    <apex:outputText>{!c.lastName}</apex:outputText>
                    <apex:outputText>{!c.email}</apex:outputText>
                    </apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
    
</apex:page>

 

 

 

Controller------------------

 

 

 

public class newOpportunityController {

    
   Account account;
   public List<Contact> contact{get;set;}
   public String selectedContact{get; set;}
   public List<SelectOption> contactList{get;set;}
   public Contact contacts{get; set;}
   
   
   public newOpportunityController()
   {
    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 ));
        }
     }   
   }
   
   
   public Account getAccount() {
        return [select id, Name, Phone from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
    }

   public PageReference test()
   {
    if (selectedContact!=null)
    {
       contacts=[select id, firstName,lastName,email from Contact where id = :selectedContact];
    }
    return null;
   }


}

 

 

hope it'll helpu.

 

Thanks,

dmotedmote

Ok, this is great!  Now, I want to go to a new VF page and display back the information selected.  How do I get the contact info?  Do I need to include the contact id in the page reference for the new page?

 

 

<apex:page controller="newOpportunityController" tabStyle="Opportunity">
  <apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/>
  <apex:form >
    <apex:pageBlock title="Confirmation">

      <apex:pageBlockSection title="Account Information">
        <apex:outputField value="{!account.name}"/>
        <apex:outputField value="{!account.site}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contact Information">
                    <apex:dataList value="{!contacts}" var="c">
                    <apex:outputText > {!c.firstname}</apex:outputText>
                    <apex:outputText >{!c.lastName}</apex:outputText>
                    <apex:outputText >{!c.email}</apex:outputText>
                    </apex:dataList>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 

public class newOpportunityController {

    public PageReference step2() {
      PageReference pageRef = new PageReference('https://c.cs1.visual.force.com/apex/davidpage2?id=' + ApexPages.currentPage().getParameters().get('id'));
      pageRef.setRedirect(true);
     
      return pageRef;

    }


    
   Account account;
   public List<Contact> contact{get;set;}
   public String selectedContact{get; set;}
   public List<SelectOption> contactList{get;set;}
   public Contact contacts{get; set;}
   
   
   public newOpportunityController()
   {
    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 ));
        }
     }   
   }
   
   
   public Account getAccount() {
        return [select id, Name, Phone from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
    }

   public PageReference test()
   {
    if (selectedContact!=null)
    {
       contacts=[select id, firstName,lastName,email from Contact where id = :selectedContact];
    }
    return null;
   }


}

 

David