+ Start a Discussion
mbforcembforce 

Need help with pageBlockTable

Hi All,

In pageBlockTable I want to add few fields from Account and few fields from Contact I have used Account as a standard controller how to give the column values for pageblocktable,please let me know if extension will be required.?

 

In table I want columns for Account Name & Billing address from account object and Name & Email id from Contact object. which are associated with that account.

what will be Value and Var details in PageBlockTable, if possible please provide me some example.

 

Thanks,

Manish.

ericmonteericmonte

Since you are using the Account as a standard controller, I am assuming you want to retrieve Contact records that are the child of Accounts, correct?

 

You can either do it two ways:

 

Scenario 1:

You can use the apex:related List component and reference the contacts. This scenario does not require extension.

 

Scenario 2:

If you want some other custom information in your pageBlock Table and contains other logic, then use an extenstion to retrieve the child records of the Account.

mbforcembforce

Thanks for the reply Eric,

 

can you guide me how to go and code with Scenario 2:

 

Thanks,

Manish.

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Manish,

 

Try the following example. There is no need to go for controller or extensions for single account and to display its related contacts. Also the Billing address cannot be directly accessed, so i used like BillingCity alone, if you want you can add any fields of address.

 

VF PAGE:
<apex:page standardController="Account">
  <apex:form >

<!--For Account Details-->
 <apex:pageblock >
    <apex:pageBlockTable value="{!Account}" var="a">
        <apex:column Headervalue="Account Name">
             <apex:outputText value="{!a.Name}"/>
         </apex:column>
        <apex:column Headervalue="Billing Address">
            <apex:outputText value="{!a.BillingCity}"/>
        </apex:column>
    </apex:pageBlockTable>
  </apex:pageblock>
<br/><br/>
<!--For Account Details-->
   <apex:pageblock >
       <apex:pageBlockTable value="{!Account.contacts}" var="c">
            <apex:column Headervalue="Contact name">
                 <apex:outputText value="{!c.name}"/>
             </apex:column>
            <apex:column Headervalue="Email">
                <apex:outputText value="{!c.Email}"/>
            </apex:column>
       </apex:pageBlockTable>
   </apex:pageblock>

 </apex:form>
</apex:page>

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

mbforcembforce

Thanks Kamatchi for a quick help,

 

Actually I want to code this with extension, could you please help me with that.

 

Thanks,

Manish.

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Manish,

 

Following is the same page using Extensions. Check this,

 

EXTENSIONS:

 

public class AccountRLcontactsController{
    public Account acc{get;set;}
    List<Account> accList = new List<Account>();
    List<contact> conList = new List<contact>();    
    public AccountRLcontactsController(Apexpages.StandardController controller)
    {
       acc = (Account)controller.getRecord();
    }   
    public List<Account> getAccList()
    {
        acclist = [select id,name, billingcity,billingstreet from Account where id=:acc.id];
        return acclist;
    }    
    public List<Contact> getConList(){
        conlist = [select id,name,AccountId,Email from Contact where AccountId=:acc.id];
        return conlist;
    }
}

 

VF PAGE:
<apex:page standardController="Account">
  <apex:form >

<!--For Account Details-->
 <apex:pageblock >
    <apex:pageBlockTable value="{!acclist}" var="a">
        <apex:column Headervalue="Account Name">
             <apex:outputText value="{!a.Name}"/>
         </apex:column>
        <apex:column Headervalue="Billing Address">
            <apex:outputText value="{!a.BillingCity}"/>
        </apex:column>
    </apex:pageBlockTable>
  </apex:pageblock>
<br/><br/>
<!--For Account Details-->
   <apex:pageblock >
       <apex:pageBlockTable value="{!conlist}" var="c">
            <apex:column Headervalue="Contact name">
                 <apex:outputText value="{!c.name}"/>
             </apex:column>
            <apex:column Headervalue="Email">
                <apex:outputText value="{!c.Email}"/>
            </apex:column>
       </apex:pageBlockTable>
   </apex:pageblock>

 </apex:form>
</apex:page>

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.