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
123_P123_P 

how to show multiple account and related contact on same page on search account in search box this is my code and its showing only single account and related contact at a time how to show multiple account and related contact

controller=======
public with sharing class Searchclass {

    public list<Account> acc {get;set;}
    public list<Contact> conList{get;set;}
    public string searchstring {get;set;} 
    public Boolean showSection2{get;set;}


    public Searchclass(ApexPages.StandardController controller) {
     
    }  
    public void search(){  
        if(searchstring != null && searchstring != '' ){
        showSection2= true;           
        string searchquery='select Name,AccountNumber,BillingCity,Industry from Account where Name like \'%'+searchstring+'%\'';       
        string searchquery1='select Title,Department,Phone,FirstName from Contact where Account.Name like \'%'+searchstring+'%\'';
        acc= Database.query(searchquery);
        system.debug('acc'+acc); 
        conList= Database.query(searchquery1); 
        system.debug('conList'+conList);
        }
    }
}

vf page===========
<apex:page standardController="Account"  extensions="Searchclass" >  
<apex:slds />
<apex:form > 
<apex:pageBlock >
  <apex:pageBlockSection columns="1" >
      <apex:inputText value="{!searchstring}" />
      <apex:commandButton value="Search records" action="{!search}"/>
    <apex:pageBlockTable value="{!acc}" var="c">
         <apex:column headerValue="Account" />        
         <apex:column value="{!c.Name}" style="width:250px"/>  
    </apex:pageBlockTable>   
    <apex:pageblockTable value="{!acc}" var="c">        
        <apex:column value="{!c.BillingCity}" />
        <apex:column value="{!c.AccountNumber}" style="width:250px"/>
        <apex:column value="{!c.Industry}" style="width:250px"/>
    </apex:pageblockTable>
    <apex:pageBlockTable value="{!conList}" var="con">
        <!--apex:column headerValue="Contact"/-->        
        <apex:column value="{!con.FirstName}" headerValue="Contact" />
        <apex:column value="{!con.Title}" style="width:250px" />
        <apex:column value="{!con.Department}" style="width:250px"/>
        <apex:column value="{!con.Phone}" style="width:250px"/>             
    </apex:pageBlockTable>                 
  </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Hope the day is treating you well.

Please try below code. I have checked in my org, it is working fine.

Visualforce:
<apex:page standardController="Account" extensions="RelatedContactSearchC">
    <apex:slds />
    <apex:form >
        <apex:pageBlock title="Search" >
            <apex:inputText value="{!searchstring}" />
            <apex:commandButton value="Search records" action="{!search}"/>
        </apex:pageBlock>
        <apex:pageBlock rendered="{!showSection2}" >
            <apex:pageBlockSection columns="1" > 
                <apex:pageBlockTable value="{!acc}" var="c">       
                    <apex:column value="{!c.Name}" style="width:250px"/>       
                    <apex:column value="{!c.BillingCity}" />
                    <apex:column value="{!c.AccountNumber}" style="width:250px"/>
                    <apex:column value="{!c.Industry}" style="width:250px"/>
                </apex:pageBlockTable>
                <br/>
                <apex:pageBlockTable value="{!conList}" var="con">
                    <apex:column value="{!con.FirstName}" headerValue="Contact" />
                    <apex:column value="{!con.Account.Name}" />
                    <apex:column value="{!con.Title}" style="width:250px" />
                    <apex:column value="{!con.Department}" style="width:250px"/>
                    <apex:column value="{!con.Phone}" style="width:250px"/>             
                </apex:pageBlockTable> 
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class RelatedContactSearchC {
    
    public list<Account> acc {get;set;}
    public list<Contact> conList{get;set;}
    public string searchstring {get;set;} 
    public Boolean showSection2{get;set;}
    
    
    public RelatedContactSearchC(ApexPages.StandardController controller) {
        
    }  
    
    public void search(){  
        if(searchstring != null && searchstring != '' ){
            showSection2= true;           
            string searchquery='select Name,AccountNumber,BillingCity,Industry from Account where Name like \'%'+searchstring+'%\'';       
            string searchquery1='select Title,Department,Phone,FirstName,Account.Name from Contact where Account.Name like \'%'+searchstring+'%\'';
            acc= Database.query(searchquery);
            system.debug('acc'+acc); 
            conList= Database.query(searchquery1); 
            system.debug('conList'+conList);
        }
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas​
123_P123_P
anas its showing only one account and related contact i want that if select another account it display previous selected account and related contact as well as recently selected account i want this type of functionality