3

I'm diving into visualforce and the standard controller for the first time and having trouble iterating over a single opportunity's related records. The below code produces an error:

<apex:page standardController="Opportunity">
<apex:pageBlock title="{!Opportunity.name}">
    <apex:pageBlockSection title="{!Opportunity.Id}">
        <apex:repeat value="{!Opportunity.Custom_object__c}" var="c">
            <apex:pageBlockSectionItem>
                {!c.Name}
            </apex:pageBlockSectionItem>
        </apex:repeat>
    </apex:pageBlockSection>
</apex:pageBlock></apex:page>

The error I'm receiving is as follows:

Error: Unknown property 'String.Name'

I take from this that the Visualforce is not interpreting the {!c.Name} as the array of objects I thought it was but instead as a string. I am able to save the file when I remove {!c.Name} which makes me think that is correct but, on the other hand, when substituting value="{!Opportunity.Account}" the code correctly retrieves the related Account. My instinct is that I'm not referencing the related child-objects correctly. I've been banging my head for a bit of time and was wondering if someone could provide some insight as to what is going on?

2 Answers 2

6

Your instincts seem right. The field Opportunity.Custom_object__c almost certainly contains the Id of a single custom object (unless it is a text field rather than a reference field). As such, the apex:repeat only has a string in var c.

Instead you will need to find the name of the relationship from Opportunity to your custom object. This would be the parent-to-child relationship name if you expect to have multiple items in the repeater.

Understanding Relationship Names can be helpful here. If you look at the Mother Daughter example the relationship name you would want would be Daughters__r. Note the __r suffix for the relationship.

Image

Once you have the correct relationship name, you might find the apex:relatedList control useful.

2
  • Hi Daniel (nice name) I see your answer now more fully explains the issue so I've selected your answer.
    – dannymorty
    Commented May 14, 2014 at 10:13
  • Glad to help. I was going to suggest using the Schema Builder to display the relationship, but it doesn't show the relationship names. Commented May 14, 2014 at 10:21
2

You have to use the Relation API Name and not the SObject API Name I edited your code so you can test it with Account and Contacts

<apex:page standardController="Account">
  <apex:pageBlock title="{!Account.name}">
    <apex:pageBlockSection title="{!Account.Id}">
        <apex:repeat value="{!Account.Contacts}" var="c">
            <apex:pageBlockSectionItem >
                {!c.Name}
            </apex:pageBlockSectionItem>
        </apex:repeat>
    </apex:pageBlockSection>
</apex:pageBlock></apex:page>

You could find the relation name in the Lookup/Master-Deatil field definition.

1
  • 1
    It took me a bit more fiddling but as you said, I replaced the API name with Child Relationship Name followed by __r and it worked a charm. Thanks for your help!
    – dannymorty
    Commented May 14, 2014 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .