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
Dustin MayfieldDustin Mayfield 

How to search for accounts and display record w/ contacts in VF page

I am fairly new to VF and apex development but I am learning quite a lot. One of the things we need is to create a VF page that allows us to input an Account name as a search and then output that account (or accounts if it returns more then one in the search) and then displays the record informatino along with contact details. I found another great post on the forums that had the 2nd part all figured out, but it didn't incorporate any search or input functionality. What would it take to add the ability to search for an account name using this below

Page:
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false" extensions="DisplayContact" >
  <apex:pageBlock title="Account" >
    <apex:form >
      
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">
      
        <apex:column headerValue="Account Name">
        <apex:commandLink rerender="contactDetails" value=" {!a.Name}" action="{!ContactLists}">
         
         <apex:param name="id" value="{!a.id}"/>
       </apex:commandLink> 
         </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
      </apex:pageBlockTable>
     
    </apex:form>
   
  </apex:pageBlock>
  <apex:pageBlock title="Contact">
   <apex:outputPanel id="contactDetails">
     <apex:pageBlockTable value="{!conList}" var="con" id="conlist" title="Contact">
     <apex:column value="{!con.Name}"/>
     <apex:Column value="{!con.Phone}" />
     <apex:Column value="{!con.Email}" />
     </apex:pageBlockTable>
        
  
    </apex:outputPanel>
  </apex:pageBlock>
  
</apex:page>
Controller:
 
public with sharing class DisplayContact {
    public List<Contact> conList {get;set;}
    public DisplayContact(ApexPages.StandardSetController controller) {

    }
    
    public PageReference ContactLists()
    {
    if(ApexPages.currentPage().getParameters().get('id') != null)
      conList = [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')];
     return null;
    }   

}


 
Sanjay Bhati 95Sanjay Bhati 95
Hy Dustin Mayfield,

Here is the code that you want to search the Account and its related Contact from input text.

1. VisualForce Page :
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false" extensions="DisplayContact" >
  <apex:pageBlock title="Account" >
    <apex:form id="frm">
      <apex:inputText value="{!searchKey}"/>
      <apex:commandButton action="{!fetchAccounts}" value="Search" reRender="frm"/>
      <apex:pageBlockTable value="{!accList }" var="a">
        <apex:column headerValue="Account Name" value="{!a.name}"/>
        <apex:column value="{!a.type}" />
        <apex:column headerValue="Contact">
            <apex:pageBlockTable value="{!a.contacts}" var="con">
            <apex:column headerValue="Contact Name" value="{!con.name}"/>
            <apex:column headervalue="Phone" value="{!con.phone}" />
            <apex:column headervalue="Email" value="{!con.Email}" />
        </apex:pageBlockTable>
        </apex:column>
      </apex:pageBlockTable>
    </apex:form>
  </apex:pageBlock>
</apex:page>

2. Controller :

public with sharing class DisplayContact {
    public List<Contact> conList {get;set;}
    public String searchKey {get;set;}
    public List<Account> accList {get;set;}
    public DisplayContact(ApexPages.StandardSetController controller) {   }
    public void fetchAccounts(){
        string skey = '%' + searchKey + '%';
        accList  = [select id,name,type,(Select id,name,phone,email from contacts) from account where name like : skey];
    }
}

Thanks