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
Patrick DixonPatrick Dixon 

<apex:repeat> and related lists

I have custom objects and a custom controller

 

content_Body__c is a child of content__c  There can be multiple children.

 

The controller get method looks like this:

 

    public Content__c getContent() {
        Content__c content =
            [select id, Name, Release_Date__c, Heading__c, Synopsis__c,
                (select Name, Content__c, URL_Link__c, Image__c, Description_Text__c
                from Content_Body__r order by name)
            from Content__c where id = :thisContent LIMIT 1];
        return content;
    }


and in a vf page this

 

            {!content.Name}<br/>
            {!content.Release_Date__c}<br/>
            {!content.Heading__c}<br/>
            {!content.Synopsis__c}<br/>
            <div class="article-content">
                <apex:outputText escape="false" value="{!content.content_body__r[0].Image__c}" />
                <apex:outputText escape="false" value="{!content.content_body__r[0].URL_Link__c}" />
                <apex:outputText escape="false" value="{!content.content_body__r[0].Description_Text__c}" />
             </div>


renders fine.

 

But I want to display all the related content_body__c elements, so I'm using apex:repeat, and this

 

        <apex:repeat var="cb" value="{!content.content_body__r}" >
            <div class="article-content">
                <apex:outputText escape="false" value="{!cb.Description_Text__c}" />
            </div>
        </apex:repeat>

 

gives a run-time error of

 

List has no rows for assignment to SObject
An unexpected error has occurred. Your development organization has been notified.


What am I doing wrong here?  How can there be no rows when the explicit [0] reference finds one???

Best Answer chosen by Admin (Salesforce Developers) 
Patrick DixonPatrick Dixon

I discovered that if I wrap the apex:repeat in an apex:panelGroup component, it all works fine (no idea why).  I can also use the panel Group to test for no records returned - which is a nice bonus.

 

            <apex:panelGroup layout="inlined" rendered="{!content.content_body__r.size > 0}">
                <apex:repeat var="cb" value="{!content.content_body__r}" >
                     <div class="clearfix article-content">
                        <a class="logo" href="{!cb.URL_Link__c}" target="_blank">
                            <apex:outputText escape="false" styleClass="logosize"  style="float:left"
                                value="{!cb.Image__c}" />
                        </a>
                        <apex:outputText escape="false" style="float:left" value="{!cb.Description_Text__c}" />
                     </div>
                    <br/>
                </apex:repeat>
            </apex:panelGrou
p>

All Answers

AhmedPotAhmedPot

The assignment to a single object requires the query to return a single row. This error generally comes because query doesn't return any records and you are assigning it directly to an sObject.May be you can try replacing sObject with list of sObject.

 

 

Patrick DixonPatrick Dixon

I discovered that if I wrap the apex:repeat in an apex:panelGroup component, it all works fine (no idea why).  I can also use the panel Group to test for no records returned - which is a nice bonus.

 

            <apex:panelGroup layout="inlined" rendered="{!content.content_body__r.size > 0}">
                <apex:repeat var="cb" value="{!content.content_body__r}" >
                     <div class="clearfix article-content">
                        <a class="logo" href="{!cb.URL_Link__c}" target="_blank">
                            <apex:outputText escape="false" styleClass="logosize"  style="float:left"
                                value="{!cb.Image__c}" />
                        </a>
                        <apex:outputText escape="false" style="float:left" value="{!cb.Description_Text__c}" />
                     </div>
                    <br/>
                </apex:repeat>
            </apex:panelGrou
p>

This was selected as the best answer