+ Start a Discussion
yaminiyamini 

How to display the related contacts of account in the same visual force page

Hi,

 

  I want to display the account and related contacts in the same visual force page. Actually i am achieved this like,

i am displayed the account names in first page block and am kept the command link for that account names if we click on that command link that will displayed the contacts related to that particular account in another pageblock. But, Now i want to display the account name first and all related contacts line by line. like that  i want to displat the contacts for all accounts. please any one help me how to solve this...

 

 

example

Maple Lawn Office III
8161 Maple Lawn Blvd
Fulton, MD 20759

Contact: Lydia Chandlee
G & R Management
840 First Street, NE
Washington, DC 20002

Phone: 301-807-0271
Fax: 202-898-0053
Email: 

Contract: No
Inspection Date: 4/6/2010
Inspection Type: Annual
5 Year Test: 2012
Reg/Serial #: HO1863
Service Company: Kone
Equipment Type: Passenger Hydraulic
Annual Price (per unit): $180
Semi-Annual Prince (per unit): $80

Maple Lawn Office III
8161 Maple Lawn Blvd
Fulton, MD 20759

Contact: Lydia Chandlee
G & R Management
840 First Street, NE
Washington, DC 20002

Phone: 301-807-0271
Fax: 202-898-0053
Email: 

Contract: Yes
Inspection Date: 4/6/2010
Inspection Type: Annual
5 Year Test: 2012
Reg/Serial #: HO1863
Service Company: Kone
Equipment Type: Passenger Hydraulic
Annual Price (per unit): $180
Semi-Annual Prince (per unit): $80

 

 

 

thanks,

yamini

Best Answer chosen by Admin (Salesforce Developers) 
yaminiyamini

Hi,

 

Thanks a lot. Its working

 

 

Thanks,

yamini

All Answers

bob_buzzardbob_buzzard

Can you clarify the behaviour you are looking for?

 

I.e. are you wanting to have a line per contact, with the account information repeated in the first column?  In your example, its not clear to me what is an account and what is a contact.

 

Its highly likely you'll need to use wrapper classes that represent a line in a table.

yaminiyamini
Hi, Actually my requirement is like Account name contact1 contact2 contact3 . . . --------------------------- account name contact1 contact2 . . . ------------------------------ account name contact1 like these i want the contact details for every account record.. Can u please help me how use the wrapper class for this. I tried a lot but i am not getting like these... Thanks, Yamini.
bob_buzzardbob_buzzard

So if I understand correctly, you want to display the account and all of its contacts on a single line?  

 

In that case you may not need a wrapper class at all.  If you pull back the account and its related contacts via a single SOQL call, you'll have the object graph in a usable format, and should be able to do something like the following:

 

 

<table>
  <apex:repeat value="{!accs}" 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>

 

 

yaminiyamini

Hi,

 

         i got this error

 System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Contacts.

 

could you please send me the query.

 

 

Thanks,

yamini

raju.Braju.B

Hi,

The code which you have sent is working well...But i want to dispaly username at the top of the list...that means i want

output like this....

 

I wrote a controller like:

 

 

public class accountcontact
{
public string owner{set;get;}
list<account> acclist=new list<account>();
public accountcontact()
{
for(account a:[SELECT CreatedbyID,Account.Name,(SELECT name,Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account])
{
owner=a.CreatedbyID;
acclist.add(a);
}
}
public list<account> getaccounts()
{
return acclist;
}
}

 

 

usename 1 :xxxxxxxxxx

............................................

Account name:

contact1

contact2.

.

.

..

Account name

Contact1

Contact2

Contact3

.

.

.

.

......................................................

Username2:xxxxxxxxxxxxxxxx

...........................

Account name:

contact1

contact2.

.

.

..

Account name

Contact1

Contact2

Contact3

 

 

i want out put like this i mean first list has to show the accounts and contacts which were created bu first user..and secod list has to show to account and contact details which were created by secod user....

Please help me out..

 

Thanks & Regards

 

RAju.B

bob_buzzardbob_buzzard

You'll need a wrapper class for this - a class which combines a user and all their accounts.

 

Something like the following should do it:

 

 

public class accountcontact
{
    list<AccountWrapper> wraplist=new list<AccountWrapper>();
    public accountcontact()
    {
       Map<Id, AccountWrapper> accMap=new Map<Id, AccountWrapper>();
       for(account a:[SELECT CreatedbyID,Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])
       {
           AccountWrapper accWrap=accMap.get(a.CreatedByID);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedByID, accWrap);
              accWrap.userId=a.CreatedById;
           }

           accWrap.accounts.add(a);
       }

       wrapList=accMap.values();
   }

   public list<AccountWrapper> getaccounts()
   {
      return acclist;
   }

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

 

and you can use this in the page like so:

 

 

<apex:repeat value="{!accounts}" var="accWrap">
   <apex:outputText value="User ID : {!accWrap.userId}"/>

   <apex:repeat value="{!accWrap.accounts}" var="acc">
      <apex:outputText value="Account : {!acc.Name}"/>

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

   </apex:repeat>

</apex:repeat>

 

 

 

 

raju.Braju.B

Hi,

Thanks for the code, its workring..But in the place of user name , it displaying user id.....

 

i mean it displaying 00590000000dvS6AAI...but i want the user name of that id...

Please help me....

 

Thanks & Regards

 

Raju.b

bob_buzzardbob_buzzard

Try pulling back the name of the owner, e.g.

 

 

   for(account a:[SELECT CreatedbyID, CreatedBy.Name, Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])

 

and then add UserName to the wrapper class etc.

 

 

 

raju.Braju.B

Hi,

   The code which you have sent is working. Thanx a lot. But the in the place of username it diplays the user id.

I tried with the createdby.name in query and am taken username in the wrapper class but it gives error like

 

System.StringException: Invalid id: Boggarapu Nagalakshmi 

 

Class.accountcontact1.: line 11, column 35 External entry point

 

I used code like:

 

public class accountcontact1
{
    list<AccountWrapper> wraplist=new list<AccountWrapper>();
    public accountcontact1()
    {
       Map<Id, AccountWrapper> accMap=new Map<Id, AccountWrapper>();
       for(account a:[SELECT createdby.name,CreatedbyID,Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])
       {
           AccountWrapper accWrap=accMap.get(a.CreatedBy.name);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedBy.name, accWrap);
              accWrap.username=a.CreatedBy.name;
           }
           accWrap.accounts.add(a);
       }
       wrapList=accMap.values();
   }
   public list<AccountWrapper> getaccounts()
   {
      return wraplist;
   }
   public class AccountWrapper
   {
      public string username {get; set;}
      public List<Account> accounts {get; set;}
      public AccountWrapper()
      {
         accounts=new List<Account>();
      }
   }
}
visual force page:

<apex:page controller="accountcontact1">

<table>

 

  <apex:repeat value="{!accounts}" var="accWrap">

 <tr> <td>

  <apex:outputText value="User ID : {!accWrap.username}"/>

</td></tr>

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

 

 

plese help me how to solve these.

 

Thanks,

Yamini

nagalakshminagalakshmi

Hi yamini,

 

Try like these.  i think u got the solution

 

 

public class accountcontact1
{
    list<AccountWrapper> wraplist=new list<AccountWrapper>();
    public accountcontact1()
    {
       Map<Id, AccountWrapper> accMap=new Map<Id, AccountWrapper>();
       for(account a:[SELECT createdby.name,CreatedbyID,Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])
       {
     AccountWrapper accWrap=accMap.get(a.CreatedByid);  
          // AccountWrapper accWrap=accMap.get(a.CreatedBy.name);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedByid, accWrap);
              accWrap.userid=a.CreatedByid;
           
             // accMap.put(a.CreatedBy.name, accWrap);
              accWrap.username=a.CreatedBy.name;
           }
           accWrap.accounts.add(a);
       }
       wrapList=accMap.values();
   }
   public list<AccountWrapper> getaccounts()
   {
      return wraplist;
   }
   public class AccountWrapper
   {
  public id userid {get; set;} 
      public string username {get; set;}
      public List<Account> accounts {get; set;}
      public AccountWrapper()
      {
         accounts=new List<Account>();
      }
   }
}

 

visual force page:

 

<apex:page controller="accountcontact1" renderAs="pdf">

<table>

 

  <apex:repeat value="{!accounts}" var="accWrap">

<!-- <tr> <td>

  <apex:outputText value="User ID : {!accWrap.userid}"/>

</td></tr>-->

<tr> <td>

  <apex:outputText value="User  name: {!accWrap.username}"/>

</td></tr>

 

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

 

thanks,

lakshmi.

 

bob_buzzardbob_buzzard

You still have to key the map by id to ensure you correctly set up the accounts by user id.  Should be a small change, highlighted below:

 

 

          AccountWrapper accWrap=accMap.get(a.CreatedById);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedById, accWrap);
              accWrap.username=a.CreatedBy.name;
           }
           accWrap.accounts.add(a);
       }

 

 

yaminiyamini

Hi,

 

Thanks a lot. Its working

 

 

Thanks,

yamini

This was selected as the best answer
veeru417veeru417

Hi,

Can any one tell me How to display the  above output in pageblocktable.Means  How to display the related contacts of account in the same visual force page pageblocktable.

 

Regards,

veeru

 

 

nagalakshminagalakshmi

Hi Veeru,

 

Try the code which has posted in this post. Its working fine...

 

Thanks

veeru417veeru417

Hi lakshmi ,

ya i have seen the code exicuted its working fine,but i want the output should be in pageblocktable instead datatable.

 

 

Thanks&Regards,

veeru

deepu.sandeepdeepu.sandeep

hi Bob

          its working fine k but, i want to display it dynamicallly not on page load i have a button show contacts and

i need to display contacts in PB table whnever i click a button .

can u suggest haw could i do dis.

 

Thanks in Advance

sfdcFanBoysfdcFanBoy

Has anyone done this using pageblock sections?

 

Please help.

nallurisivashankarnallurisivashankar

Hi Bob,

 

How to write test class for wrapper class in this scenario?

 

Thanks,

Shiva

 

srilakshmi1.387861669756762E12srilakshmi1.387861669756762E12
<table>
  <apex:repeat value="{!accs}" 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>
from this code i am getting all contact of a account ,but i want only one contact (with out any class)
Deepika Gupta 26Deepika Gupta 26
Hi,

My Req. is little bit diffrent as compare with it.I am displaying an account hierarchy on a visualforce page.now with every acconts it must show its related records.like :

Hiearchy->
Acc1->Con1,con2
Acc2->Con1,Con2
Acc3->Con1,Con2

Is this possible?If yes then how?
 
Shubham Panda 7Shubham Panda 7
i also have the same requirement as deepika gupta...
can anyone help me??
sfdc freshersfdc fresher
Hi Deepika gupta/ Shubhum..
you can try below code

<apex:repeat value="{!act}" var="a">
            <apex:outputText value="Account: {!a.name  },  "/> 
           
            <apex:repeat value="{!a.contacts}" var="cont">
                <apex:outputText value="Contact: {!cont.name}  "/>
            </apex:repeat>
            <br/>
            <br/>
</apex:repeat>
            
Sudheer Kumar 20Sudheer Kumar 20
Please send me code below task

In the first page, input some characters and click search button to show the search results in the below table. Besides, clicking those item links will lead to the second page to edit.
 
Umesh RathiUmesh Rathi
This might be helpful for people still looking for this kind of requirement.

Visualforce Page:
<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>

Controller:
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 it helps!
Bkumar PBBkumar PB
Hi
Can any one help me How to display the  below output in pageblocktable.How to display the user opportunities and lead in  visual force page pageblocktable

Owner   Total Leads     Total OppsConv Rate          Max  Created Date (Opp)   Total Val (Opp)
John
Smith         20                    5                    25.00%             12/15/2014                           $20,000.00
mike
jones         10                      1                    10.00%             12/12/2014                          $3,000.00
------------------------------------------------------------------------------------------------------------------------------------------

Requirements

Column                                                     Requirement
------------                                                    ----------------------
Owner                                                    Name of the user

Total Leads                                            Total Leads of owner by created date (Date filter applies to created date)

Total Opps.                                            Total Opportunities of owner by closed date where stage = Closed Won

                                                                (Date filter applies to closed date)

Conversion Rate                                    Total Opps./ Total Leads in Percentage format


Max. Created Date (Opp.)                      Created Date of the most recent Opportunity


Total Value                                               Total Values of the Opportunities
 
anusha kanteanusha kante
@Bkumar PB
hai can you help me the same Requirement