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
Tabitha Oostra 1Tabitha Oostra 1 

visualforce displaying records relatedlists apex code shows blank screen

I am trying to follow the directions for the Developer's Beginner Module, I'm on: Displaying Records, Fields and Tables. 

The instructions seem to be missing something as I can't get my apex code to display the records as indicated in the instructions or I'm doing it incorrectly and the instructions aren't missing anything. But I think there is a missing piece. 

Here's the code: 
<apex:detail standardController="Account" >
    <apex:relatedList list="Contacts"/>
    <apex:relatedList list="Opportunities" pageSize="5"/>
</apex:detail>

My Preview screen comes up blank. Your help would be appreciated to know what I'm missing. 

Thanks.
T.
Best Answer chosen by Tabitha Oostra 1
Andrew Lewis 9Andrew Lewis 9
Apologies as I misread your question, I assumed it was for the challenge. 

Hopefully the code below can help you.

<apex:page standardController="Account">
   <apex:detail relatedList="false"/>
   <apex:relatedList list="Contacts"/>
   <apex:relatedList list="Opportunities" pageSize="5"/> 
</apex:page>

All Answers

Andrew Lewis 9Andrew Lewis 9
I believe this is the module in question, 

Create a page which displays a subset of Opportunity fields using apex:outputField components. Bind the Name, Amount, Close Date and Account Name fields to the apex:outputField components.
The page must be named 'OppView'.
It must reference the Opportunity standard controller.
It must have an apex:outputField component bound to the Opportunity Name.
It must have an apex:outputField component bound to the Opportunity Amount.
It must have an apex:outputField component bound to the Opportunity Close Date.
It must have an apex:outputField component bound to the Account Name of the Opportunity.

In this case you would not need to use apex:detail. 
Looking at the request we need to use the opportunity standard controller so we can access the fields.
We also want to use a block section to display thie fields requested. 
It should look like th ecode below. 

<apex:page standardController="Opportunity">
   
    <apex:pageBlock title="Opportunities">
     <apex:pageBlockSection >
         <apex:outputField value="{! Opportunity.Name}"/>
         <apex:outputField value="{! Opportunity.Amount}"/>
         <apex:outputField value="{! Opportunity.CloseDate}"/>
         <apex:outputField value="{! Opportunity.Account.Name}"/>
       
      </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Please note the reason your page is blank is because you are using <apex:detail standardController="Account" > not 
<apex:page standardController="Account" >.

Let me know if this helps and mark as best answer if it does :)
Tabitha Oostra 1Tabitha Oostra 1
Thanks Andrew, but that is one sections after this one... here's the instructions:

Display Related Lists
Use <apex:relatedList> to display lists of records related to the current record.
The <apex:detail> component displays the details of a particular record, as well as lists of related records, such as contacts, cases, opportunities, and so on. This might be too much, so you can remove those related lists, and then add back just a few, using a different coarse-grained component.
Revise the markup to omit related lists by adding relatedList="false" to the <apex:detail> component.
   1 <apex:detail relatedList="false"/>
The account record’s details are still displayed, but the related lists are gone.
Below the <apex:detail/> line, add the following markup.
  1 <apex:relatedList list="Contacts"/>
  2 <apex:relatedList list="Opportunities" pageSize="5"/>
Your page should now display two related lists, for contacts and opportunities. Also notice that you can configure each related list independently, by changing attributes on just that component.
The <apex:relatedList> component is another coarse-grained component, but it’s lower level than <apex:detail>. That is, <apex:detail> shows many related lists at once (or none), while <apex:relatedList> lets you go one by one. This lets you show only the related lists that you’re interested in, and you can customize the display of each related list individually.

I just can't seem to get this to work and here is the way I laid out the apex code. I'm not sure what I'm doing wrong. 

       Here's the code I used: 
       <apex:detail standardController="Account" >
       <apex:relatedList list="Contacts"/>
       <apex:relatedList list="Opportunities" pageSize="5"/>
       </apex:detail>

Thanks,
T.
Andrew Lewis 9Andrew Lewis 9
Apologies as I misread your question, I assumed it was for the challenge. 

Hopefully the code below can help you.

<apex:page standardController="Account">
   <apex:detail relatedList="false"/>
   <apex:relatedList list="Contacts"/>
   <apex:relatedList list="Opportunities" pageSize="5"/> 
</apex:page>
This was selected as the best answer
Tabitha Oostra 1Tabitha Oostra 1
No worries. Thank you for your help. I think the instructions are confusing as they always say "replace" or I must be reading it wrong. 

Much appreciated!
T.