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
hoomelhoomel 

populating contact list based on account lookup

Hello,

 

i am currently trying to create a Visualforce page which extends the "Send an Email"-functionality.

What I would like to do is create a lookup field for an account and then automatically populate a selectList with the related contacts to that account.

 

Is this in any way possible? The most challenging thing is assigning the selected account to a variable to query the list of contacts from it. Everything else should be fairly basic stuff.

 

Maybe someone has a hint for me.

 

Regards,

hoomel

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

See this example and use the code , 

 

Controller 

 

public class ContactListController{

    public Contact proxyContactForAccLookup {get;set;}
    public ContactListController()
    {
        proxyContactForAccLookup = new Contact();
        lstCon = new List<Contact>();
    
    }
    
    public String AccountId {get;set;}
    Public List<Contact> lstCon {get;set;}
    public PageReference getContactList()
    {
        AccountId  = proxyContactForAccLookup.AccountId;
        if(AccountId != null)
        {
            lstCon = [Select Name from Contact where Accountid =: AccountId];
        }
        
        return null;
    }
    
}

 

 

VFP

 

<apex:page controller="ContactListController">
  
  <apex:form>
      <apex:pageBlock>
          <script>
              function getcontact()
              {
                  jsGetContact();
              }
          </script>
          
          <apex:actionFunction action="{!getContactList}"
                    rerender="contactTable" name="jsGetContact"/>
           <apex:pageBlockSection>
               <apex:pageBlockSectionItem>
               <apex:outPutLabel value="Account Name" ></apex:outPutLabel>
               <apex:inputField value="{!proxyContactForAccLookup.AccountId}" onchange="getcontact();" />
               
               </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
      
           <apex:pageBlockSection columns="1" id="contactTable">
               <apex:pageBlockTable value="{!lstCon}" var="con">
                   <apex:column value="{!con.Name}"/>
               </apex:pageBlockTable>
           </apex:pageBlockSection>
      
      </apex:pageBlock>
  
  </apex:form>
  
  
</apex:page>

 let me know with you results.

All Answers

Shashikant SharmaShashikant Sharma

See this example and use the code , 

 

Controller 

 

public class ContactListController{

    public Contact proxyContactForAccLookup {get;set;}
    public ContactListController()
    {
        proxyContactForAccLookup = new Contact();
        lstCon = new List<Contact>();
    
    }
    
    public String AccountId {get;set;}
    Public List<Contact> lstCon {get;set;}
    public PageReference getContactList()
    {
        AccountId  = proxyContactForAccLookup.AccountId;
        if(AccountId != null)
        {
            lstCon = [Select Name from Contact where Accountid =: AccountId];
        }
        
        return null;
    }
    
}

 

 

VFP

 

<apex:page controller="ContactListController">
  
  <apex:form>
      <apex:pageBlock>
          <script>
              function getcontact()
              {
                  jsGetContact();
              }
          </script>
          
          <apex:actionFunction action="{!getContactList}"
                    rerender="contactTable" name="jsGetContact"/>
           <apex:pageBlockSection>
               <apex:pageBlockSectionItem>
               <apex:outPutLabel value="Account Name" ></apex:outPutLabel>
               <apex:inputField value="{!proxyContactForAccLookup.AccountId}" onchange="getcontact();" />
               
               </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
      
           <apex:pageBlockSection columns="1" id="contactTable">
               <apex:pageBlockTable value="{!lstCon}" var="con">
                   <apex:column value="{!con.Name}"/>
               </apex:pageBlockTable>
           </apex:pageBlockSection>
      
      </apex:pageBlock>
  
  </apex:form>
  
  
</apex:page>

 let me know with you results.

This was selected as the best answer
hoomelhoomel

Thank you, that helped me a lot.

 

It has a little flaw as you posted it, as I get an Unknown Property 'VisualforceArrayList.Name' error on the contact list. When I remove the .Name it shows the correct number of objects, but it does not seem to get through to the name.

 

But this is not a problem for me, since I need to create a new SelectOption from the resulting contacts anyway.

 

Thanks again for your help!

Shashikant SharmaShashikant Sharma

Your Welcome Mate

noobfnnoobfn

hello,

I'm a noob to VP and I'm trying to change your code a little so that when I click a button I get the contacts from the Account ID entered in the text field.  Can I just use the standard controller, how to invoke this?