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
Nirmal9114Nirmal9114 

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>
Best Answer chosen by Nirmal9114
Abhishek_DEOAbhishek_DEO
Please copy the below code (as it is) in page "VFviewcontact" and see it it works


<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

Nirmal9114Nirmal9114
THIS IS THE VIEW I AM GETTING AFTER TRYING LOT OF THINGS
AshlekhAshlekh
Hi,

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.
<apex:page Controller="ViewContacts" >
<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>

-Thanks
Ashlekh Gera
Nirmal9114Nirmal9114
Hi,

After putting extension ="ViewContacts" same view its showing
Abhishek_DEOAbhishek_DEO
Tanya,
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
Nirmal9114Nirmal9114
Hi abhishek,

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 

 
Abhishek_DEOAbhishek_DEO
Hi Tanya,

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
Nirmal9114Nirmal9114
Hi Abhishek,

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.
User-added image




 
Abhishek_DEOAbhishek_DEO

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

Abhishek_DEOAbhishek_DEO
Also, you don't need to use any custom code(extension/controller). It can be achieved by using standard controller as I mentioned in my previous post. just use the same code.

account
Nirmal9114Nirmal9114

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.

 
Abhishek_DEOAbhishek_DEO
Please copy the below code (as it is) in page "VFviewcontact" and see it it works


<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>
This was selected as the best answer
Abhishek_DEOAbhishek_DEO
I am assuming that you are still using "ViewContacts" in your vf page. It has no use . To access related contact you can use relationship fields "{!account.contacts}"
Nirmal9114Nirmal9114
Thanku so Much for help it working now :)