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
RiyasRiyas 

How to get custom child objects using standardcontroller ?

Hi,

 

  I have two custom objects Blog & Comments, for each Comment, there is a parent Blog. So I have create a custom field of type Master-Detail Relationship for Comments  object with field name Blog_Post and relation ship name Comment_r

 

Now I want to show a Blog post and its associated comments in a page.  It is showing error while I am try to get all the comments for a blog post. The code used as follows:

<apex:page showHeader="false" standardcontroller="Blog__c">
<apex:repeat value="{!Blog__c.Comment__r}" var="comment">
                        <div>
                            <label>{!comment.Comment_User__c} 
                                <small>(
                                    <apex:outputText value="{0,date, EEE MMM d yyyy h:mm a}">
                                        <apex:param value="{!comment.Comment_date__c}" /> 
                                    </apex:outputText>
                                )</small>
                            </label>
                            <br /><br />
                            <p>{!comment.Comment_text__c}</p>
                            <br />  
                        </div>                  
                        </apex:repeat>
</apex:page>

 

 

I got the error as follows 

 

        

Description Resource Path Location Type
Save error: Invalid field Comment__r for SObject Blog_Post__c blogview.page /Blog/src/pages line 0 Force.com 

 

How can I show the blog with its comments?

 

 

 

 

SFDC_EvolveSFDC_Evolve

Please share the Function also where u query the data ....:)

RiyasRiyas

 Hi SFDC_Evolve,

 

        Thanks for your replay.  I didn't use any fuctions or custom controller to query the data. I have used the standaredcontroller for my custom object  Blog_Post__c. I am try to get the comments through relationship.

 

 

aballardaballard

Are you sure the relationship name is comment__r?   Not comments__r ?

Somya TiwariSomya Tiwari
To my best of knowledge i can say that relationshiip name for Comment's custom object must be Comments__r not comment__r. Kindly have a look at the following code.
<apex:page showHeader="false" standardcontroller="Blog__c">
    <apex:repeat value="{!Blog__c.Comments__r}" var="comment">
        <div>
            <label>{!comment.Name} 
                    <apex:outputText>
                        <apex:param value="{!comment.Comment__c}" /> 
                    </apex:outputText>
            </label>
        </div>                  
    </apex:repeat>
</apex:page>


Kindly mark the answer as Best Solution if it helped you :)
Dushyant SonwarDushyant Sonwar
Go to master detail field and copy the child relationship name and add '__r' to it.

I am giving example for contacts.User-added image

Hope this helps.
Deepali KulshresthaDeepali Kulshrestha
Hi Riyas,
Greetings to you!

You need to add an extension to get the child on the parent object.

Here Contact is parent and Site_visit1__c is the child.
=================================================================================================
<apex:page standardController="Site_Visit1__c" extensions="extendSiteVisit" recordSetVar="sites">
<apex:pageBlock >
<apex:pageBlockTable var="s" value="{!sites}"> 
<apex:column headerValue="Name of Visit" > 
<apex:outputField value="{! s.Name}"/> 
</apex:column> 
<apex:column headerValue="Date of Visit"> 
<apex:outputField value="{! s.Time_and_Date__c}"/> 
</apex:column> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:page> 

public class extendSiteVisit { 
public List<Site_Visit1__c> sites{get; set;} 
public Opportunity Site_Visit1__c{get; set;}

    public extendSiteVisit(ApexPages.StandardSetController controller) {
         currentRecord = [SELECT Id, Name, contact__c FROM site_visit1__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; // this will return current record object(child) detail from that you can use parent Id (here contact is parent so contact__c)
         
     sites = [SELECT id, Time_and_Date__c, name, contact__c FROM site_visit1__c WHERE conact__c=:currentRecord.accountId]; 
    } 

}


================================================================================================================================

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.


Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com