1

I am having account table with name,email,.. fields. If i click on one account my requirement is to display its contacts in a table format.

My code is:

<apex:page controller="custable" sidebar="false">
    <apex:form >
        <apex:pageBlock title="AccountTable">
            <apex:pageBlockTable value="{!Acclst}" var="A">
            <apex:column headerValue="EDIT">
            <apex:outputLink value="/{!A.id}/e" id="edit">Edit</apex:outputLink>
            </apex:column>
            <apex:column headerValue="DELETE">
            <apex:commandLink action="{!deleteAccount}" onclick="if(!confirm('Are you sure?')) return false;">Del
            <apex:param value="{!A.Id}" name="idToDel" assignTo="{!recid}"/>
            </apex:commandLink>
            </apex:column>
            <apex:column headerValue="NAME OF THE ACCOUNT" > 
            <apex:commandLink value="{!A.Name}" action="/{!A.id}"/>
            </apex:column>  
            <apex:column value="{!A.Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

controller is:

public with sharing class custable {

     public string recid{get;set;}
       public string row{ get; set;}
       public list<Account> Acclst{get;set;}
       Public Account A;



       public custable(){

       Acclst = [select Id,Name from Account ];

}
       public pagereference deleteAccount()
       {
       account ac=[select id,name from account where id=:recid];
       delete ac;


       pagereference ref =new pagereference('/apex/custable');
       ref.setredirect(true);
       return ref;

}
}

previously i redirected to record detail page. But now requirement is to display contacts table when i click on a account record.

1
  • <apex:commandLink value="{!A.Name}" action="/003?rlid=RelatedContactList&id={!A.Id}"/>
    – Frederik
    Apr 29 '14 at 7:44
3

You'll need the following for this:

  1. A list of contacts property in the controller
  2. A table that iterates a list of contacts from the controller
  3. An action method that retrieves the list of contacts for the specified account
  4. A commandlink that executes the action method, passing the account id into the controller, and then rerenders the table of contacts

Something like the following:

Controller:

public with sharing class custable {

     public string recid{get;set;}

   public string row{ get; set;}
   public list<Account> Acclst{get;set;}
   Public Account A;

   public List<Contact> contacts {get; set;}

   public custable(){

   Acclst = [select Id,Name from Account ];
   contacts=null;
   }

   public pagereference deleteAccount()
   {
   account ac=[select id,name from account where id=:recid];
   delete ac;


   pagereference ref =new pagereference('/apex/custable');
   ref.setredirect(true);
   return ref;

  }

  public void setupContacts()
  {
     contacts=[select id, FirstName, LastName from Contact where AccountId=:recId];
      }
   }

Page:

<apex:page controller="custable" sidebar="false">
    <apex:form >
        <apex:pageBlock title="AccountTable">
            <apex:pageBlockTable value="{!Acclst}" var="A">
            <apex:column headerValue="EDIT">
            <apex:outputLink value="/{!A.id}/e" id="edit">Edit</apex:outputLink>
            </apex:column>
            <apex:column headerValue="DELETE">
            <apex:commandLink action="{!deleteAccount}" onclick="if(!confirm('Are you sure?')) return false;">Del
            <apex:param value="{!A.Id}" name="idToDel" assignTo="{!recid}"/>
            </apex:commandLink>
            </apex:column>
            <apex:column headerValue="NAME OF THE ACCOUNT" > 
            <apex:commandLink value="{!A.Name}" action="{!setupContacts}" rerender="conttable">
              <apex:param value="{!A.Id}" name="idForConts" assignTo="{!recid}"/>
            </apex:commandLink>
            </apex:column>  
            <apex:column value="{!A.Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!contacts}" var="contact" id="conttable">
        <apex:column value="{!contact.id}"/>
        <apex:column value="{!contact.FirstName}"/>
        <apex:column value="{!contact.LastName}"/>
      </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
3
  • pagereference ref =new pagereference('/apex/custable'); The page reference redirect to record detail page but contact records are not displaying in table for contacts. Apr 29 '14 at 9:11
  • Sorry - mistake in my code - return type should be void. The page shouldn't change, because of the rerender attribute. Instead it will just update the table - I've updated the action method signature. Apr 29 '14 at 10:54
  • 1
    I've tweaked the contact bit slightly, as it looked awful - its now a pageblocktable. I've verified this exact page and controller is working in my dev org. Do you have any contacts defined for the account you are selecting? Apr 29 '14 at 11:06
0

@Bob,I'm trying to display contacts under the same account(on click of + symbol for expand) in the form of table not at the bottom of the account table.But particular account columns also coming side by side of contact table.

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.