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
Prateek Aneja 2Prateek Aneja 2 

Child object record display

I have created an object "Employee" and child object "Customer".

I have written an Apex Code that'll fetch details of employees and customers:
**********************************************************************************************************************
public class contrBinding {
    public List<Employee__c> employee{get;set;}
    
    public List<Employee__c> getEmpl(){
       
            employee = [select Name, First_Name__c, Last_Name__c, (select Name, First_Name__c, Last_Name__c from Customers__r) from Employee__c limit 10];
            return employee;
    }
}
*******************************************************************************************


This is the Visualforce page code:
*******************************************************************************************
<apex:page controller="contrBinding" sidebar="false">
  <apex:form >
      <apex:pageBlock title="Employee Details">
          <apex:pageBlockTable value="{!Empl}" var="emp">
              <apex:column value="{!emp.Name}"/>
              <apex:column value="{!emp.First_Name__c}"/>
              <apex:column value="{!emp.Last_Name__c}"/>
          </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>
*******************************************************************************************************

Can anyone tell me how can I bring the Customer recods on Visualforce page?

 
Mikhail Muzo 7Mikhail Muzo 7
You will need to create a map of Customer Records in Apex very similar to the list you have of employees
I say map because you already have a list of Employees and in order to capture the child records for each employee you need to query for the Employee Id  on those records.
Also the map is useful to organize them by employee. this way you can have a list of Customers for each Employee in you "Empl" List
 
public Map<Id,Customer__c> customersMap {get;set;}

public List<Employee__c> getCust(){
    List<Id> employeeIds = new List<Id>();

    for(Employee__c emp: employees){
        employeeIds.add(emp.Id);
    }

    //fetch all customers data related to ALL the employees in the list
    List<Customer__c> customers = [select FIELDS from Customer__c where Employee__c IN employeeIds];
    
    customersMap = new Map<Id,Customer__c>();

    //building the map structure the key is the Employee Id 
    //the value is a list of customers related to that Employee
    for(Customer__c cust: customers){

        //new Employee Entry
        if(customersMap.containsKey(c.Employee__c){
            List<Customer__c> custList = new List<Customer__c>();
            custList.add(cust);
            customersMap.put(c.Employee__c,custList);
        }
        else{
            //to avoid the list of customers to be deleted we grab the list and add the new customer
            List<Customer__c> custList = customersMap[c.Employee__c];
            custList.add(cust);
            
            //after that we overwrite the old list in the map
            customersMap.put(c.Employee__c,custList);
        }
    }
    
    return customersMap;
}
Once you have the Customers' map ready
in visual force you can grab the list of custmers by adding something like this
 
<apex:page controller="contrBinding" sidebar="false">
  <apex:form >
      <apex:pageBlock title="Employee Details">
          <apex:pageBlockTable value="{!Empl}" var="emp">
              <apex:column value="{!emp.Name}"/>
              <apex:column value="{!emp.First_Name__c}"/>
              <apex:column value="{!emp.Last_Name__c}"/>

              <apex:colum value="{!Cust[emp.Id]}" />

          </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>
This will create a new Column with the information of all Customers related to that Employee

Im pretty sure you would like to organize it differently in the table but that would be up to you.

I Hope this helps