[SalesForce] Visualforce: Displaying A Custom Object’s Related List When the Custom Object Isn’t Directly “Related”

I've been tasked with creating an executive page layout where the basic information is displayed within a standard page layout and if they'd like to see more details they can click a link and see that.

That translates into a Visualforce page. I've gotten as far as creating the page, but where I'm stumped is displaying the multiple custom object related lists. When I added them at first, I started to get the following error

" ' is not a valid child relationship name for entity Account'
"

After looking at this, I realised that several of the custom objects are related to the Account by way of other standard objects. An example of this is my Account Connections object.

It has a Master-Detail relationship to the Contact object and can be viewed on the standard Account detail page. How would I go about adding this custom object's related list into my VF code? This is what I have thus far:

<apex:page standardController="account">
<apex:sectionHeader title="Account" subtitle="{!account.FirstName}"/>
<apex:pageBlock title="Account">

    <apex:pageBlockSection title="Account Information" columns="2">
        <apex:outputField title="Primary Relationship Manager" value="{!account.Primary_Relationship_Manager__c}"/>
        <apex:outputField title="Account Owner" value="{!account.OwnerId}"/>
        <apex:outputField title="Tax Exempt" value="{!account.Tax_Exempt__c}"/>
        <apex:outputField title="Account Record Type" value="{!account.RecordTypeId}"/>
        <apex:outputField title="EIN" value="{!account.EIN_Number__c}"/>
        <apex:outputField title="Account Name" value="{!account.Name}"/>
        <apex:outputField title="Type" value="{!account.Type}"/>
        <apex:outputField title="Parent Account" value="{!account.ParentId}"/>
        <apex:outputField title="Tocqueville City" value="{!account.Tocqueville_City__c}"/>
        <apex:outputField title="Preferred Name" value="{!account.Preferred_Name__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Description Information" columns="1">
        <apex:outputField title="Description" value="{!account.Description}"/>
        <apex:outputField title="Best Work Desc - 2014 CIPS" value="{!account.Best_Work_Desc_2014_CIPS__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Address & Contact Information" columns="2">
        <apex:outputField title="Phone" value="{!account.Phone}"/>
        <apex:outputField title="Secondary Phone" value="{!account.Secondary_Phone__c}"/>
        <apex:outputField title="Email" value="{!account.Email__c}"/>
        <apex:outputField title="Toll Free" value="{!account.TollFree__c}"/>
        <apex:outputField title="Secondary Email" value="{!account.Secondary_Email__c}"/>
        <apex:outputField title="Fax" value="{!account.Fax}"/>
        <apex:outputField title="Email Recipient" value="{!account.Email_Recipient__c}"/>
        <apex:outputField title="Call Before Fax" value="{!account.Call_Before_Fax__c}"/>
        <apex:outputField title="Website" value="{!account.Website}"/>
        <apex:outputField title="Pledge URL" value="{!account.Pledge_URL__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Additional Address Information" columns="2">
        <apex:outputField title="Other Street" value="{!account.Other_Street__c}"/>
        <apex:outputField title="Other City" value="{!account.Other_City__c}"/>
        <apex:outputField title="Other State/Province" value="{!account.Other_State_Province__c}"/>
        <apex:outputField title="Other Zip/Postal Code" value="{!account.Other_Zip_Postal_Code__c}"/>
        <apex:outputField title="Other Country" value="{!account.Other_Country__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Membership Information" columns="2">
        <apex:outputField title="Membership Status" value="{!account.UWA_Membership_Status__c}"/>
        <apex:outputField title="Segment" value="{!account.Segment__c}"/>
        <apex:outputField title="UW Org Number" value="{!account.UWOrgNumber__c}"/>
        <apex:outputField title="UW Metro Size" value="{!account.UW_Metro_Size__c}"/>
        <apex:outputField title="UW Org Number (old)" value="{!account.UW_Org_Number_old__c}"/>
        <apex:outputField title="UW Metro Alpha Code" value="{!account.Metro_Alpha_Code__c}"/>
        <apex:outputField title="Opportunity 150" value="{!account.Critical_150__c}"/>
        <apex:outputField title="Relationship Management Tier" value="{!account.Top_Strategic_Field_Relations__c}"/>
        <apex:outputField title="Major Market" value="{!account.Major_Market__c}"/>
        <apex:outputField title="RD Top Market" value="{!account.RD_Top_Market__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Social Media" columns="2">
        <apex:outputField title="YouTube URL" value="{!account.YouTube_URL__c}"/>
        <apex:outputField title="Twitter" value="{!account.Twitter__c}"/>
        <apex:outputField title="Vimeo URL" value="{!account.Vimeo_URL__c}"/>
        <apex:outputField title="Facebook Page" value="{!account.Facebook_Page__c}"/>
        <apex:outputField title="Vine URL" value="{!account.Vine_URL__c}"/>
        <apex:outputField title="Facebook Page Name" value="{!account.Facebook_Page_Name__c}"/>
        <apex:outputField title="Other Video Sharing URL" value="{!account.Other_Video_Sharing_URL__c}"/>
        <apex:outputField title="Tumblr URL" value="{!account.Tumblr_URL__c}"/>
        <apex:outputField title="Flickr URL" value="{!account.Flickr_URL__c}"/>
        <apex:outputField title="Pinterest URL" value="{!account.Pinterest_URL__c}"/>
        <apex:outputField title="Picasa Albums URL" value="{!account.Picasa_Albums_URL__c}"/>
        <apex:outputField title="LinkedIn URL" value="{!account.LinkedIn_URL__c}"/>
        <apex:outputField title="Instagram URL" value="{!account.Instagram_URL__c}"/>
        <apex:outputField title="Google+ URL" value="{!account.Google_URL__c}"/>
        <apex:outputField title="Other Photo Sharing URL" value="{!account.Other_Photo_Sharing_URL__c}"/>
        <apex:outputField title="Blog URL" value="{!account.Blog_URL__c}"/>
        <apex:outputField title="Social Media Council (SMC)" value="{!account.Social_Media_Council__c}"/>
        <apex:outputField title="SMC Start Date" value="{!account.SMC_Start_Date__c}"/>
        <apex:outputField title="SMC End Date" value="{!account.SMC_End_Date__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Campaign for the Common Good" columns="1">
        <apex:outputField title="CCG Participant" value="{!account.CCG_Participant__c}"/>
        <apex:outputField title="One Million Online Engagement Agreement" value="{!account.CCG_Terms_Conditions_Agreement__c}"/>
        <apex:outputField title="Company Affinity Groups" value="{!account.program_involvement__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Volunteerism" columns="1">
        <apex:outputField title="Volunteer Management Platform" value="{!account.Volunteer_Matching_Application__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Education Involvement" columns="2">
        <apex:outputField title="Education Focus Areas" value="{!account.Education_Focus_Areas__c}"/>
        <apex:outputField title="Other Education Focus Areas" value="{!account.Other_Education_Focus_Areas__c}"/>
        <apex:outputField title="Mobilization Dimensions" value="{!account.Mobilization_Dimensions__c}"/>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="System Information" columns="2">
        <apex:outputField title="Account Division" value="{!account.Division}"/>
        <apex:outputField title="name ID" value="{!account.IMIS_name_ID__c}"/>
        <apex:outputField title="SalesforceId" value="{!account.SalesforceId__c}"/>
        <apex:outputField title="UWO Id" value="{!account.uwoId__c}"/>
    </apex:pageBlockSection>

        <apex:relatedList list="Contacts"/>
        <apex:relatedList list="Account Connection"/>
        <apex:relatedList list="OpenActivities"/>
        <apex:relatedList list="ActivityHistories"/>
        <apex:relatedList list="Cases"/>
        <apex:relatedList list="CombinedAttachments"/>

</apex:pageBlock>

I'm somewhat under a time crunch, so any help you the community can provide me is greatly appreciated! Thank you so much.

Diavonna

Best Answer

As you said your object has contact as Master-Detail relationship in that case it is bit tricky to display them on Account layout. Reason being Account is itself master on Contact object it means multiple contact can be child of Account so if you want to show childs of Contact object, you don't know which contact's child it is.

As an alternative option you can create Account lookup on your custom object and fill that field with Account id in that way you will get relationship name which you can use directly on your Account related list.

so for example if you have a custom object named Obj_Child__c which has Contact as Master after creating Account as lookup with named Account__c your relationship name will be

<apex:relatedList list="Contact_child__r"/>