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
SV MSV M 

Retrieve contacts related to account

Hi,

I would like to retrieve the contacts related to each account using the apex class. I tried but I am getting the list of accounts on page. Can someone tell me how to achieve this?

//Apex Class

public class operations_class
{
    public List<Contact> result{get;set;}
    public List<Account> accList = [Select Id from Account];
    public operations_class()
    {
        result=new List<Contact>();
    }
    public void search()
    {
        result=[Select FirstName, LastName, Email from Contact Where account.Id=:accList];
    }
}


//VF Page

<apex:page controller="operations_class">
    <script>
        window.onloadstart = function()
        {
            hello()
            {

            }
        }
    </script>
    <apex:form>
        <apex:actionFunction name="hello" action="{!search}">
        <apex:pageBlock>
            <apex:pageBlockTable value="{!result}" var="con">
                <apex:column value="{!con.FirstName}"/>
                <apex:column value="{!con.LastName}"/>
                <apex:column value="{!con.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        </apex:actionFunction>
    </apex:form>
</apex:page>
Foram Rana RForam Rana R
Hi Vineeth,

Plaese Try below code.
VFP:
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock>
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText  value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>
Class:
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

Hope this will help you.
Please Mark it as best answer if you resolve the issue.

Thanks,
Foram Rana

 
SV MSV M
I want to use ActionFunction can you do that using the actionFunction tag.
Foram Rana RForam Rana R
There is no need of Action function:
You can also use below code as well.
No need to write controller.
<!-- Page: -->

<apex:page standardController="Account">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!account.Contacts}" var="item">

            <apex:column value="{!item.name}"/>

        </apex:pageBlockTable>

    </apex:pageBlock>

</apex:page>

 
sachinarorasfsachinarorasf
Hi Sai,

I have gone through your problem.
Apex class: 
public class operations_class
{
    public List<Contact> result{get;set;}
    public List<Account> accList = [Select Id from Account];
    public operations_class()
    {
        result=new List<Contact>();
        result=[Select FirstName, LastName, Email from Contact Where account.Id IN: accList];
    }
    
}

Vf Page:
<apex:page controller="operations_class">
  <apex:pageBlock >
    <apex:form >
      <apex:pageBlockTable value="{!result}" var="contactAccountVar" id="list">
        <apex:column value="{!contactAccountVar.FirstName}"/>
        <apex:column value="{!contactAccountVar.LastName}"/>
        <apex:column value="{!contactAccountVar.Email}"/>
      </apex:pageBlockTable>
    </apex:form>
  </apex:pageBlock>
</apex:page>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora