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
Sale IT UPSale IT UP 

Not able to display the Related Child record on Visualforce-Page

APEX code:

public with sharing class InnerSOQLQuery {
    
    
    public List<Invoice__c> invList {get;set;}
    
   public List<Item_Detail__c> itemDetail {get;set;}
    
    public  void getRelatedRecords(){
   
     itemDetail = new List<Item_Detail__c>();
            
 invList = [SELECT Invoice_Number_c, Product_Detailsc, (SELECT Name, Quantityc, Ratec FROM Item_Detailsr LIMIT 7) FROM Invoice_c WHERE Id = 'a015w00001FG4gz'];
      
        for(Invoice__c Invs:invList){
            
            for( Item_Detail_c itm:Invs.Item_Details_r){
                itemDetail.add(itm);
                system.debug('Item Name-->'+ itm.Name+ '-->Rate-->'+itm.Rate_c + '-->Quantity-->'+itm.Quantity_c);
            }
        }  
        system.debug('Item Name-->'+ itemDetail);
       
    }
}

VF Code>

<apex:page controller="InnerSOQLQuery"   docType="html-5.0" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="true">

  <apex:pageBlock title="My Content">
    <apex:repeat value="{!invList}" var="acct">
        <apex:outputText value="{!acct.Product_Details__c}"/>
        
        <apex:repeat value="{!acct.Item_Details__r}" var="obj2">
            <apex:outputText value="{!obj2.Name}"/>
        </apex:repeat>
    </apex:repeat>
</apex:pageBlock>
</apex:page>

Result:

Blank Page

But my Controller shows proper Output while executing via Anonymous window:
It fetches Child records
 
Best Answer chosen by Sale IT UP
David Zhu 🔥David Zhu 🔥
You may need to add action attribute to apex:page component. This will triggers getRelatedRecords method when the page is initialized.

<apex:page controller="InnerSOQLQuery"   docType="html-5.0" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="true" action="{!getRelatedRecords}">

All Answers

David Zhu 🔥David Zhu 🔥
You may need to add action attribute to apex:page component. This will triggers getRelatedRecords method when the page is initialized.

<apex:page controller="InnerSOQLQuery"   docType="html-5.0" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="true" action="{!getRelatedRecords}">
This was selected as the best answer
Sale IT UPSale IT UP
Thank you David for prompt response!
Yes! your solution worked for me :)
Sale IT UPSale IT UP

@David Zhu 

Adding further query to esixting thread:

In below SOQL query, I do not wish to provide hardcode Record ID instead want to fetch Record ID from VF page.

invList = [SELECT Invoice_Number_c, Product_Detailsc, (SELECT Name, Quantityc, Ratec FROM Item_Detailsr LIMIT 7) FROM Invoice_c WHERE Id = 'a015w00001FG4gz'];

In that case how should I proceed?

I tried below code, but it failed on VF page:

APEX-->

public with sharing class InnerSOQLQuery {
       
 public Id InvId;
    public List<Invoice__c> invList {get;set;}
   
   public List<Item_Detail__c> itemDetail {get;set;}
    
    public  void getRelatedRecords(String InvId){
   
     itemDetail = new List<Item_Detail__c>();
            
 invList = [SELECT Invoice_Number_c, Product_Detailsc, (SELECT Name, Quantityc, Ratec FROM Item_Detailsr LIMIT 7) FROM Invoice_c WHERE Id =: InvId];
      
        for(Invoice__c Invs:invList){
            
            for( Item_Detail_c itm:Invs.Item_Details_r){
                itemDetail.add(itm);
                system.debug('Item Name-->'+ itm.Name+ '-->Rate-->'+itm.Rate_c + '-->Quantity-->'+itm.Quantity_c);
            }
        }  
        system.debug('Item Name-->'+ itemDetail);
       
    }
}

Error on VF Page:
User-added image


Please help me!

David Zhu 🔥David Zhu 🔥
You may Remove signature from this method.
    public  void getRelatedRecords(String InvId){
   
=》    public  void getRelatedRecords(){

You already have variable invid defined in your class.
You can either set in in class instantiation method or in getrelatedrecords method