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
SFDC16SFDC16 

how to get all contact names of particular one Account in apex soql

I want fetch all contact names of perticular account in apex class 

List<Contact > lis=[SELECT Id, Name FROM Contact where Account.Id = '0017F00000hvpw4'];
System.debug('lis'+lis); 

 
Best Answer chosen by SFDC16
Leafen SandyLeafen Sandy

Then you should go with a wrapper class.

try the below,
 
<apex:page controller="viewcontacts">
<apex:form >
<apex:pageblock >
<apex:commandButton value="Show Contacts" action="{!selectaccount}" reRender="condisp"/>
<apex:pageblocksection columns="2">
<apex:pageBlockTable value="{!accounts}" var="c">
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!c.accselect}"/>
</apex:column>
<apex:column value="{!c.acc.Name}"/>
</apex:pageBlockTable>



<apex:pageblockTable value="{!consellist}" var="a" id="condisp">
<apex:column value="{!a.name}"/>

</apex:pageblockTable>

</apex:pageblocksection>
</apex:pageBlock>



</apex:form>
</apex:page>

and the controller,
 
public class viewcontacts {
public list<contactwrapper> accounts{get;set;}
public list<contactwrapper> contactwrapperlist {get;set;}
public list<contact> conlist{get;set;}
public list<contact> consellist{get;set;}
public viewcontacts()
{
accounts= new list<contactwrapper>();

for(account at:[select id, name from account ])
{

accounts.add(new contactwrapper(at));
}
}



public void selectaccount()
{

contactwrapperlist= new list<contactwrapper>();
conlist=new list<contact>();
consellist=new list<contact>();
conlist=[select id, name,AccountId from contact];
for(contactwrapper cw : accounts)
{
for(contact c: conlist)
{
if(cw.accselect==true && cw.acc.id==c.accountid)
{
contactwrapperlist.add(cw);
consellist.add(c);
}
}
}}


public class contactwrapper
{
public boolean accselect{get;set;}
public account acc{get;set;}
public contactwrapper(account a)
{
accselect=false;
acc=a;
}
}
}

Let me know if you get any errors.

All Answers

Rahul.MishraRahul.Mishra
Hi,

Update your query to:
List<Contact > lis=[SELECT Id, Name ROM Contact where AccountId = '0017F00000hvpw4'];

Mark solved if it does help you.
SFDC16SFDC16
Thank you Its' working....
SFDC16SFDC16
If I click on account name check box I want all related contact name of particular account List name =[Select id from Account]; for(Account a:Name) { List lis=[SELECT Id, Name FROM Contact where Account.Id = :a.id]; System.debug('lis'+lis); }
Leafen SandyLeafen Sandy
Need more clarity, but what I understood is you have a table of accounts with checkbox in each row, which if you select has to list down all its related contact.
Is this what you require.
 
SFDC16SFDC16
Yes...
Leafen SandyLeafen Sandy

Then you should go with a wrapper class.

try the below,
 
<apex:page controller="viewcontacts">
<apex:form >
<apex:pageblock >
<apex:commandButton value="Show Contacts" action="{!selectaccount}" reRender="condisp"/>
<apex:pageblocksection columns="2">
<apex:pageBlockTable value="{!accounts}" var="c">
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!c.accselect}"/>
</apex:column>
<apex:column value="{!c.acc.Name}"/>
</apex:pageBlockTable>



<apex:pageblockTable value="{!consellist}" var="a" id="condisp">
<apex:column value="{!a.name}"/>

</apex:pageblockTable>

</apex:pageblocksection>
</apex:pageBlock>



</apex:form>
</apex:page>

and the controller,
 
public class viewcontacts {
public list<contactwrapper> accounts{get;set;}
public list<contactwrapper> contactwrapperlist {get;set;}
public list<contact> conlist{get;set;}
public list<contact> consellist{get;set;}
public viewcontacts()
{
accounts= new list<contactwrapper>();

for(account at:[select id, name from account ])
{

accounts.add(new contactwrapper(at));
}
}



public void selectaccount()
{

contactwrapperlist= new list<contactwrapper>();
conlist=new list<contact>();
consellist=new list<contact>();
conlist=[select id, name,AccountId from contact];
for(contactwrapper cw : accounts)
{
for(contact c: conlist)
{
if(cw.accselect==true && cw.acc.id==c.accountid)
{
contactwrapperlist.add(cw);
consellist.add(c);
}
}
}}


public class contactwrapper
{
public boolean accselect{get;set;}
public account acc{get;set;}
public contactwrapper(account a)
{
accselect=false;
acc=a;
}
}
}

Let me know if you get any errors.
This was selected as the best answer
SFDC16SFDC16
Excellent It's working fine as per my requirement.. Thank you very much for the reply.
Charlie Jones 4Charlie Jones 4
I don't think it's possible to get what you want with just one Soql query. With SOQL you can access the parent and/or the children of the object you are querying, but you can't access the parent's other children. Starting with opportunity and querying through account to contacts is not possible. www.krogerfeedback.com (https://krogerfeedback.me/www-krogerfeedback-com-get-fuel-points-kroger-feedback/)