1

I am new to Visuaforce, and realize it might be a basic question. ANy reference or guidance would be helpful...THanks in advance.

My requirement is-----> On a page i want a Section wise display of Accounts and its corresponding Contacts.Which i am able to do using apex:PageBlock. E.g: Section 1 Header displays account name as Account 1 and beneath would show its contacts . Below Section 1, shows section 2 for Account 2 and its corresponding contacts and so on..

I am facing an issue when i am trying to display multiple accounts.And issue is i am not understanding how to pass the ListOfAccountId's to COntact function(for returning the list of Corresponging contacts),as looping through the accountId's and passing individual id's as a signature to the Contactfunction is leading to a syntax error.

pasting the code of Page and Ctrller:--

enter image description here

enter image description here

  • Use apex:repeat control salesforce.com/us/developer/docs/pages/Content/… – NullReferenceException Aug 18 '14 at 16:00
  • Was able to fix the issue by modifying the controller .But now rather than displaying all the expected 10 records of Accounts(along with contacts)..it is displaying only one record,which is the last index(1oth) of the fetched account-contact relation.. How shall the data be made to display from 0th record till 9th one. – user10727 Aug 19 '14 at 0:03
1

Try the following code:

Apex Class:

public class DispAccountCont
{
    public List<AccContacts> contactList{get;set;}
    public class AccContacts
    {
       public string Acc{get;set;}
       public List<Contact> contacts{get;set;}
    }
    public DispAccountCont()
    {
       List<Account> accs =[select name,Id from account limit 10];
       contactList =new List<AccContacts>();
       for(Account a : accs)
       {
         AccContacts a =new AccContacts();
         a.Acc = a.Name;
         a.contacts = getChildContacts(a.Id); 
         contactList.add(a); 
       }
    }
    public List<Contact> getChildContacts(string id)
    {
        return [Select name,phone,state__c,country__c from Contact where accountid =: id Limit 1000]; 
    }
 }

Visualforce Page:

<apex:page controller="DispAccountCont">
   <apex:form id="form">
      <apex:pageblock > 
           <apex:repeat value="{!contactList}" var="rec">
              <apex:outputText value="{!rec.Acc}" "></apex:outputText>
              <apex:PageBlockTable value="{!rec.contacts}" var="item">
                  <apex:column value="{!item.Name}"/>
                  <apex:column value="{!item.phone}"/>
                  <apex:column value="{!item.State__c}"/>
                  <apex:column value="{!item.Country__c}"/>
             </apex:PageBlockTable>
          </apex:repeat>
       </apex:pageblock > 
    </apex:form>
</apex:page>

Hope it helps you

  • I am new to visualforce,so tried copying your code and it gave below error, I tried using my controller and page by hardcoding the account Id and the custom function ChildContact() works fine...But In your code I didnt understand the concept of creating a user defined class for AccContacts and how is it helping in here. Error: common.exception.SfdcSqlException: ORA-00001: unique constraint (CORE.AK2APEX_CLASS) violated ORA-06512: at "SLEEPY.CAPEX", line 907 ORA-06512: at line 1 {call cApex.update_class(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)} {call cApex.update_class(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}, – user10727 Aug 18 '14 at 18:19
  • 1
    @user10727 apex:repeat is an iteration component that allows you to output the contents of a collection according to a structure that you specify.Using this control you can iterate your Account and display Contact list of each Account.PageblockTable works only for one Account,if you want to show all accounts with contacts you have to use apex:repeat,here <apex:outputText> display your Account Name and ContactList displays in the PageBlockTable of each Account. go through this link salesforce.com/us/developer/docs/pages/Content/… – NullReferenceException Aug 19 '14 at 5:24
0

you can use apex:repeat and add apex:pageblock under apex:repeat. your apex:repeat's values attribute will bind to List of Accounts. for each account, you will have separate pageblockTable.

  • Using apex:Repeat with apex:outputText doesnt give the data formatting the way apex:column gives. And i am able to show the List of contacts using apex:pageBlock as well. But i am facing an issue in passing the account id's to the respective Contact query. I have checked that i cannot pass the account Id as an signature to ChildContacts()..and am struggling to find a work around for this and working on it....Any suggestion?? As i am a beginner and not sure how should the syntax be. – user10727 Aug 18 '14 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.