You need to sign in to do that
Don't have an account?

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>
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>
Hope the day is treating you well.
Please try below code. I have checked in my org, it is working fine.
Visualforce:
Controller:
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