Saturday, May 9, 2009

What should go in the List parameter for a <apex:relatedList List=""> tag?

To answer this question, I decided to build a VisualForce page that would be as close as possible to the pages created using the standard page layouts.

For this task, I created two custom objects to represent a flight. One custom object would contain information about the flight and the second custom object would have information for the city. This picture ilustrates how I set up the relationships between them.

This is how I defined each of my objects:

This is the definition for "City":

Nothing special here, but note there is a field called "ABC" (it will be important when I explain the rules for naming child relationships)

This is the definition for "Flight":

There are some basic fields ("Arrival","Departure", ...) and there are two lookup fields to "City". One for the "Origin" and one for the "Destination".

This is how I defined the relationships:

This image illustrates how the "Origin" field is defined:

Note there is an error in the definition of the "Child Relationship Name".

This entry is very important to answer this post's question. These are the rules to the values you can enter:

  1. Only alphanumeric characters (Letters, numbers and underscore) are allowed
  2. Must begin with a letter
  3. Can’t end with an underscore
  4. Can’t contain two consecutive underscore characters.
  5. Must be unique across all city fields
    1. Can not be "ABC", because there is one field named "ABC" in City. I defined this field, to explain this point.
    2. Can not be the name of a different relationship… Although it may not be technically accurate, it helps if you think these relationships create a field in the destination object (City in this case).
With these rules, I used these "child relationship names":

  • Origin_Flights
  • Destination_Flights

Now I can answer this blog's question:

<apex:relatedList list="Origin_Flights__r" />
<apex:relatedList list="Destination_Flights__r" />
Simulating a standard page layout in VisualForce

My next step, create a page that looked as closed as possible to the ones using standard page layouts. Basically, this was my goal:

As you can see, there are a lot of standard related lists. How could I get their names? Eclipse will tell you:

With that information, I created a page that looked as close to the standard page layout as I could.

Before I show you the results, let me tell you there are some differences, but I will leave them for a future post. For example, I did not created the JavaScript to show the related lists Divs on mouse over. The section header is not identical (some links are missing)

This is what I got:

Finally, here is the code...

VisualForce Page:

<apex:page standardController="City__c" showHeader="true" tabStyle="City__c">
    <apex:sectionHeader title="City" subtitle="{!City__c.Name}" help="/help/doc/user_ed.jsp?loc=help&target=getstart_help.htm§ion=Getting_Started" />
    This page is made in visualFroce simulating a Page Layout Page... This is the description... blah... blah... blah<br/><br/>
    <apex:detail relatedList="false" title="false" />
    <apex:relatedList list="OpenActivities"/>
    <apex:relatedList list="ActivityHistories"/>
    <apex:relatedList list="NotesAndAttachments"/>
    <apex:relatedList list="Origin_Flights__r" />
    <apex:relatedList list="Destination_Flights__r" />
    <apex:relatedList list="ProcessSteps"/>
    
<H1>Other Related Lists Available from VisualForce</H1>

    <apex:pageblock title="City History">
        <apex:pageBlockTable value="{!City__c.Histories}" var="h">
            <!-- h.ID, h.ParentId -->
            <apex:column headerValue="Date">
                <apex:outputField value="{!h.CreatedDate}"/>
            </apex:column>
            <apex:column headerValue="User">
                <apex:outputField value="{!h.CreatedById}"/>
            </apex:column>
            <apex:column headerValue="Field">
                <apex:outputText value="{!h.Field}"/> <!-- This is a picklist -->
            </apex:column>
            <apex:column headerValue="From">
                <apex:outputField value="{!h.OldValue}"/>
            </apex:column>
            <apex:column headerValue="To">
                <apex:outputField value="{!h.NewValue}"/>
            </apex:column>
            <apex:column headerValue="IsDeleted">
                <apex:outputField value="{!h.IsDeleted}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageblock>
    <apex:pageblock title="City Tags">
        <apex:pageBlockTable value="{!City__c.Tags}" var="t">
            <!-- t.Id, t.ItemId-->
            <apex:column headerValue="TagDefinitionId">
                <apex:outputField value="{!t.TagDefinitionId}"/>
            </apex:column>
            <apex:column headerValue="CreatedDate">
                <apex:outputField value="{!t.CreatedDate}"/>
            </apex:column>
            <apex:column headerValue="SystemModstamp">
                <apex:outputField value="{!t.SystemModstamp}"/>
            </apex:column>
            <apex:column headerValue="IsDeleted">
                <apex:outputField value="{!t.IsDeleted}"/>
            </apex:column>
            <apex:column headerValue="Name">
                <apex:outputField value="{!t.Name}"/>
            </apex:column>
            <apex:column headerValue="Type">
                <apex:outputField value="{!t.Type}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageblock>
</apex:page>

No comments:

Post a Comment