+ Start a Discussion
lnryanlnryan 

apex:relatedList: relationship is not recognized

I noticed that my Visualforce page threw the following error when I tried to add a related list that was perfectly well defined in the object's setup but happened not to be in the Page Layout: 'SubContracts__r' is not a valid child relationship name for entity Licensing Contract

This appears to be an undocumented restriction on the use of <apex:relatedList> not to mention one that could potentially cause some nasty issues in a production environment should an admin update a page layout or create a new one after the orginal page is deployed. Likewise, if they create new recordtypes or recordtype/profile assignments. This restriction is not intuitive; one generally thinks of Visualforce as an alternative to a Page Layout, not as an extension that is bound to it and inherents its content restrictions. Additionally, for developers who have over-ridden all page-layouts for an object with VF pages, it is counter-intuitive to have to edit a Page Layout that is not in use simply to include the list in their VF pages.

Could someone respond back with some clearer details on how this actually works? Does Visualforce simply require that one page layout include the related list or does it require the page layout for the given record type include it. Wouldn't it be more robust to simply define the apex:related list to display based on user permissions rather than throw an error if the list is not found in the related page layout.


Message Edited by lnryan on 12-16-2008 01:12 PM
Best Answer chosen by Admin (Salesforce Developers) 
ShikibuShikibu

 

  • The related list must be present on the page layout (otherwise apex:relatedList won't know what columns/fields to display, right?).
  • The "child relationship name" for the Lookup field on the child object must be specified (Setup/Create/Custom Objects, edit the field, look near bottom right). If your custom child object is named "MyObject", the  child relationship name should be "MyObjects". 

 

 


<apex:page standardController="MyParentObject">
<apex:pageBlock title="This is the related list">

<apex:relatedList list="MyChildObjects__r" />

</apex:pageBlock>

</apex:page>

Message Edited by Shikibu on 03-18-2009 11:57 PM

All Answers

waylonatcimwaylonatcim
I was having a similar issue with related lists, where I kept getting the same error that you are and I started thinking the same thing, that the relationship could not be displayed without being in the page layout.  I eventually found through apex explorer that the related list name had namespace prefix to it that was causing the issue.  So my suggestion would be to check the child relationships in apex explorer and look for any prefixes on the names.
lnryanlnryan
Name-space prefixes can be ruled out as the issue. Firstly, my case does not use managed package objects, only in-house custom objects, so they're all within the same name-space (according to the APEX documentation); Secondly, in this case, it's an internal relationship to the same object so it's guarenteed that the child is in the same namespace as the parent; Finally, the issue was resolved by adding the list to the page layout. So, my question was not so much 'Are Visualforce related lists dependent on Page Layout Settings ?' as 'Why and in What specific way are Visualforce related lists depending on Page Layout Settings?' Sorry if I was not clear before.
 
1. why is this the case?
2. are there any plans to change it?
3. what exactly is the limitation exhibited by this behavior?
 
 


Message Edited by lnryan on 12-17-2008 08:58 PM
Rusty12Rusty12

I just ran into the same thing for the NotesAndAttachments related list on the Lead object.  Needless to say, I agree that this is not very friendly behavior.

 

hamayoun65hamayoun65

I think the reason for this is that a related list has a list of fileds associated with it.  The only place where that list can be specified is on a Page Layout.  So no Page Layout, and no way of knowing what fields to dsiplay for the related list.

 

The good news: you can create something very similar to a related list using PageBlockTable.  This is from the VF manual: I have tried this with a Custom Object related to Accounts, and it works fine too.

 

Some Visualforce components, such as <apex:pageBlockTable> or <apex:dataTable>, allow you to display information from multiple records at a time by iterating over a collection of records. To illustrate this concept, the following page uses the <apex:pageBlockTable> component to list the contacts associated with an account that is currently in context:

<apex:page standardController="Account">
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the {!account.name} account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.Contacts}" var="contact">
<apex:column value="{!contact.Name}"/>
<apex:column value="{!contact.MailingCity}"/>
<apex:column value="{!contact.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

 

ShikibuShikibu

 

  • The related list must be present on the page layout (otherwise apex:relatedList won't know what columns/fields to display, right?).
  • The "child relationship name" for the Lookup field on the child object must be specified (Setup/Create/Custom Objects, edit the field, look near bottom right). If your custom child object is named "MyObject", the  child relationship name should be "MyObjects". 

 

 


<apex:page standardController="MyParentObject">
<apex:pageBlock title="This is the related list">

<apex:relatedList list="MyChildObjects__r" />

</apex:pageBlock>

</apex:page>

Message Edited by Shikibu on 03-18-2009 11:57 PM
This was selected as the best answer
jwetzlerjwetzler
Almost.  If your custom child object is named 'MyObject', the child relationship name will be whatever you specify on your custom field definition in the Child Relationship Name field.  With a '__r' added to it.  It should default to the plural version of your object but it's editable.
ShikibuShikibu
Thanks for the clarification, Jill. I encountered this problem while trying to access a custom object that was created for my org in 2006, apparently by Salesforce at our initial roll-out (before my time). It had the Child Relationship Name blank!
JJenkinsJJenkins

What about for standard objects?  I'm trying to tab out the accounts/ leads/ opportunity related lists but when trying to put in Partners or Related Content I receive the not a valid child relationship error.

 

Any help would be appreciated.

 

Thanks,

2Wrongs2Wrongs

In my case, SF named my object something bizarre internally.  The custom object I was trying to use was named 'Accounts_Receivable', but somewhere SF decided it should be referenced as Accounts_Receivable1__r.  Nowhere in my naming is there a '1'.

 

The only way I figured it out was installing the app 'EasyPage' (great app BTW) from app exchange and creating a detail page copy of Account.

 

SanchSanch

I had a very similar issue, but for me, this was the case:

 

I had a custom object that had a look up relationship to Account. For the Account, I created a custom visualforce page and added the custom object as a related list. The user's profile have full permission on the custom object that I created, I have added the custom object as a related list to all the page layouts for the account. But it was still throwing the exception. I found that even though you add to the page layout, if the user customize the page to remove the relatedlist for them self, it will still throw an exception.

 

I know we could check if the current user have access to the object, this helps, but if the pagelayout does not have the related list or if the user removes it from the display, it will throw an exception. I was wondering if there is a way to check if the page layout has the object as a related list and if we can check if the user have removed it from the related lists as well. Even better, when we check the permission, that permmission should check for all these cases and return true if it's in pagelayout and if it's in user's related list, otherwise return false.

 

 

DisruptiveStartupDisruptiveStartup

I have given the customer portal user rights to 2 custom objects (ParentObject and ChildObject).

 

Both objects have the "Customer Portal Enabled" checkbox checked.

All the necessary fields in field level security are ticked for the profile.

The related list is on the page layouts.

 

The page is as follows;

 

<apex:page standardController="ParentObject" >

 

        <apex:relatedList list="ChildObjects__r" />

</apex:page>

 

 

Viewing this page as system admin looks fine.

Viewing this page as a customer portal user returns the following error:

" 'ChildObjects__r' is not a valid child relationship name for entity ParentObject"

 

Does anyone have any ideas ?