You need to sign in to do that
Don't have an account?

not able to populate contacts of account
APEX CLASS
public class ViewContacts
{
public List<contact> acc{get;set;}
public ViewContacts (){
searchresult();
}
Public void searchresult(){
SYSTEM.DEBUG('Number of records ');
acc = [SELECT Name,lastName,phone,id FROM Contact WHERE name != null LIMIT 999 ];
}
}
VF PAGE
<apex:page standardController="Account" >
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!acc}" var="con">
<apex:column value="{!con.Name}" />
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
public class ViewContacts
{
public List<contact> acc{get;set;}
public ViewContacts (){
searchresult();
}
Public void searchresult(){
SYSTEM.DEBUG('Number of records ');
acc = [SELECT Name,lastName,phone,id FROM Contact WHERE name != null LIMIT 999 ];
}
}
VF PAGE
<apex:page standardController="Account" >
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!acc}" var="con">
<apex:column value="{!con.Name}" />
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:page standardcontroller="account" >
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.{!account.name}
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.contacts}" var="con">
<apex:column value="{!con.Name}" />
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
All Answers
Just check you are using Acc in pageblock table but the visualforce page don't bind with controller in which you have written acc in ViewContacts class.
-Thanks
Ashlekh Gera
After putting extension ="ViewContacts" same view its showing
You don't need to use extension as you are not extending any standard controller in apex:page component. As AKG has mentioned, you should use Controller.
For eg.
<apex:page Controller="ViewContacts" >
It should work
Thanks for pointing out my mistake.It worked.
But I want it to be on Account object, which will redirect user to VF page populating all the contacts, associated with the Account.
That was the reason behind using standardController
For that you don't need to write any extension/controller class. It all can be done with standard controller only.
Look at below eg.
<apex:page standardcontroller="account" >
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.{!account.name}
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.contacts}" var="con">
<apex:column value="{!con.Name}" />
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
To see the page, acces follwoing url: https://<yourserverURL>.visual.force.com/apex/<pagename>?id=00120000018cZJx
Remember, you have to pass an accountID as a paremeter in page
I have created a custom button on account detail page as "view contact" which will show all the contacts associated with it.
My VF page code is now :-
<apex:page standardController="Account" extensions="ViewContacts">
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockButtons >
<apex:commandButton value="Delete Contacts" action="{!Delete}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!acc}" var="con">
<apex:column value="{!con.Name}"/>
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex code being same as above.
Then also it is not fetching the data.
what URL you are accessing?
You have to pass an ID in URL
for eg. if page name is "showcontact" the it should be /showcontact?id=15digitaccountID
This is wat its getting re-directed to
https://c.ap2.visual.force.com/apex/VFviewcontact?scontrolCaching=1&id=0012800000Ch52j
I even tried your way
https://c.ap2.visual.force.com/apex/VFviewcontact?id=0012800000Ch52j
Its giving same output as the screenshot above.
<apex:page standardcontroller="account" >
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the account.{!account.name}
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.contacts}" var="con">
<apex:column value="{!con.Name}" />
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>