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
bujjibujji 

Contacts rleated to Accounts

Hi Guys,

 

I have scenario like this, I have to display all the contacts related Accounts in visual force page.

 How to write th Apex class for this.

 

Thanks,

Shaik

bob_buzzardbob_buzzard

I wrote a blog post on this topic (although it allowed editing of the contact data too) - see if that gets you started:

 

http://bobbuzzard.blogspot.co.uk/2011/04/edit-parent-and-child-records-with.html

bujjibujji
Hi,

 

I want in this format.

  Express Logistics and Transport
First NameLast NamePhoneEmail
BabaraLevy(503) 421-7800b.levy@expressl&t.net
JoshDavis(503) 421-7800j.davis@expressl&t.net
GenePoint
First NameLast NamePhoneEmail
EdnaFrank(650) 867-3450efrank@genepoint.com
Grand Hotels & Resorts Ltd
First NameLast NamePhoneEmail
JohnBond(312) 596-1000bond_john@grandhotels.com
TimBarr(312) 596-1000barr_tim@grandhotels.com
Pyramid Construction Inc.
First NameLast NamePhoneEmail
PatStumuller(014) 427-4427pat@pyramid.net
United Oil & Gas Corp.

First NameLast NamePhoneEmail

ArthurSong(212) 842-5500asong@uog.com
AviGreen(212) 842-5500agreen@uog.com
LaurenBoyle(212) 842-5500lboyle@uog.com
StellaPavlova(212) 842-5500spavlova@uog.com
Eugene PozniakEugene Pozniak

Configure Map<Account, List<Contact>> in the controller, that will contain Account as key and related Contacts as value.

 

Then on the page add something like this:

 

<apex:page>
	. . . 
	<apex:repeat value="{!yourMap}" var="account">
		<apex:pageBlockSection title="account.Name">
			<apex:pageBlockTable value="{!yourMap[account]}" var="contact">
				. . .
				<!-- Add columns which you need -->
				. . .
			</apex:pageBlockTable>
		</apex:pageBlockSection>
	</apex:repeat>
	. . .
</apex:page>

 

bujjibujji

Hi,

 

Please send me the query using map.I am getting error.

 

Thanks,

Bujji

Eugene PozniakEugene Pozniak

Try this:

 

Map<Account, List<Contact>> accountToContacts = new Map<Account, List<Contact>>();
List<Account> accounts = [
        SELECT Name,
            (SELECT FirstName, LastName, Phone, Email
            FROM Contacts LIMIT 2000)
        FROM Account LIMIT 50000];

for(Account acc : accounts) {
    if(acc.Contacts != NULL && (!acc.Contacts.isEmpty())) {
        accountToContacts.put(acc, acc.Contacts);
    }
}

 

bujjibujji

Hi,

 

The query is not getting any values i checked in debug console.

I am sending code please check this.

In the related list i am printing account. But i want contacts related to page block section accounts in related list.

 

 

<apex:page standardController="Account" extensions="ExtensionPage" showHeader="false">

<apex:pageBlock id="pb" title="Accountd With Contacts">
    <apex:repeat value="{!records}" var="r" >
    <apex:pageBlockSection title="{!r.Name}" collapsible="false" />
    
         <apex:pageBlockTable value="{!r}" var="contact">
                <apex:column headerValue="Account Name" >{!contact.Name}</apex:column>
                <apex:column headerValue="Phone"  >{!contact.phone}</apex:column>
                <apex:column headerValue="Billing State/Province" >{!contact.BillingState}</apex:column>
                <apex:column headerValue="Website" >{!contact.Website}</apex:column>
         </apex:pageBlockTable>
    
    </apex:repeat>
</apex:pageBlock>

</apex:page>

 

*******************************************************************************************************

 

public class ExtensionPage {

    public ExtensionPage(ApexPages.StandardController controller) {

    }
      
    public List<Account> getRecords(){    
        List<Account> accs = [Select Id,Name,phone,BillingState,Website From Account Limit 5];
        
        return accs;
    }
    
}

 

 

Thanks,

Shaik