0

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>
1
  • How are you planning to display contact against each account? from User's point-of-view. Is there going to be a list of account, against one of them you want contact displayed? or is it gonna be contacts against each account on account page?
    – Mahmood
    Feb 26, 2020 at 16:29

2 Answers 2

2

You can achieve this using standard controller as @Abhishek mentioned

<apex:page standardController="Account" recordSetVar="Acct">
    <apex:pageBlock>
        <table>
            <apex:repeat value="{!Acct}" var="acc">
                <tr>
                    <td><apex:outputText value="{!acc.Name}"/></td>
                    <apex:repeat value="{!acc.Contacts}" var="cont">
                        <td><apex:outputText value="{!cont.Name}"/></td>
                    </apex:repeat>
                </tr>
            </apex:repeat>
        </table>
    </apex:pageBlock>
</apex:page>

OR

If you want to do this using Custom Controller then you can achieve this using Wrapper Class - See below code:

public class operations_class{
    list<AccountWrapper> accountWrapperList = new list<AccountWrapper>();
    public operations_class(){
       Map<Id, AccountWrapper> accountMap = new Map<Id, AccountWrapper>();
       for(account acc :[SELECT CreatedbyID,Account.Name,(SELECT name,Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account]){
           AccountWrapper accountWrap = accountMap.get(acc.CreatedByID);
           if (null==accountWrap){
              accountWrap = new AccountWrapper();
              accountMap.put(acc.CreatedByID, accountWrap);
              accountWrap.userId=acc.CreatedById;
           }

           accountWrap.accounts.add(acc);
       }

       accountWrapperList = accountMap.values();
   }

   public list<AccountWrapper> getAccounts()
   {
      return accountWrapperList;
   }

   public class AccountWrapper
   {
      public Id userId {get; set;}
      public List<Account> accounts {get; set;}
      public AccountWrapper()
      {
         accounts=new List<Account>();
      }
   }
}

VF Page

<apex:page controller="operations_class">
    <table>
        <apex:repeat value="{!accounts}" var="accWrap">
            <apex:repeat value="{!accWrap.accounts}" var="acc">
                <tr>
                    <td>
                        <apex:outputText value="Account : {!acc.Name}"/>
                    </td>
                </tr>

                <apex:repeat value="{!acc.contacts}" var="cont">
                    <tr>
                        <td>
                            <apex:outputText value="Contact : {!cont.Name}"/>
                        </td>
                    </tr>    
                </apex:repeat>

                <tr>
                    <td>
                        <hr/>
                    </td>
                </tr>
            </apex:repeat>
        </apex:repeat>
    </table>
</apex:page>
0
<apex:page controller="operations_class">
<script>
    window.onload = 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>

Have a look at it, it might solve your problem Just replace window.onloadstart by window.onload.

2
  • I tried the same but still, I am getting the only list of contacts. Is it possible to achieve the result using apex:actionFunction
    – user78606
    Feb 25, 2020 at 8:53
  • 2
    For Account list, you can achieve this functionality using standard controller, there is no need to use apex:actionFunction.
    – Abhishek56
    Feb 25, 2020 at 9:28

You must log in to answer this question.