+ Start a Discussion
GirinoobGirinoob 

How to display subquery value in visualforce page

Hi,

I have master detail relationship with contact and custom object BroadBandCustomers .

So, i have used a subquery to fetch few details from custom object . i would like to know how to display sub query value in visualforce page . I am getting values .... tested in workbench. 

Here is my query:

SELECT Homeroom__c ,Phone  ,Number_of_calls__c ,

(SELECT id FROM BroadBandCustomers__r ORDER BY HSI_Stage_Date__c)

 FROM Contact  ORDER BY LastName, FirstName

 

Best Answer chosen by Admin (Salesforce Developers) 
AshlekhAshlekh

Hi,

I wish this will help you.


    List<Contact> c = [SELECT Homeroom__c ,Phone  ,Number_of_calls__c ,(SELECT id FROM BroadBandCustomers__r ORDER BY HSI_Stage_Date__c) FROM Contact  ORDER BY LastName, FirstName];
    Now you have a list of contact . Now make a map because each contact may have more than one broadbandcustomers. A map contain id of each contact respective list of bbcustomer
    Map<String,List<BroadBandCustomer__c>> mapC = new  Map<String,List<BroadBandCustomer__c>>()
    for(Contact x : c){
        mapC.add(c.id, c.BroadBandCustomers__r);
    }
    
    <apex:repeat value="{!C}" var="M">  
            <apex:repeat value="{!MyMap[M.id]}" var="temp">  
                    <apex:outputLabel value="{!temp.id}"/>  
            </apex:repeat>  
    </apex:repeat>

 

/* If answered Mark as solved it might help someone in need */

All Answers

Nilesh ManeNilesh Mane

Use Contact.BroadBandCustomers__c in value attribute.

GirinoobGirinoob

It says invalid field name in contact object when i use contact.BroadBandCustomers__c

AshlekhAshlekh

Hi,

I wish this will help you.


    List<Contact> c = [SELECT Homeroom__c ,Phone  ,Number_of_calls__c ,(SELECT id FROM BroadBandCustomers__r ORDER BY HSI_Stage_Date__c) FROM Contact  ORDER BY LastName, FirstName];
    Now you have a list of contact . Now make a map because each contact may have more than one broadbandcustomers. A map contain id of each contact respective list of bbcustomer
    Map<String,List<BroadBandCustomer__c>> mapC = new  Map<String,List<BroadBandCustomer__c>>()
    for(Contact x : c){
        mapC.add(c.id, c.BroadBandCustomers__r);
    }
    
    <apex:repeat value="{!C}" var="M">  
            <apex:repeat value="{!MyMap[M.id]}" var="temp">  
                    <apex:outputLabel value="{!temp.id}"/>  
            </apex:repeat>  
    </apex:repeat>

 

/* If answered Mark as solved it might help someone in need */

This was selected as the best answer
Tushar MahadikTushar Mahadik
Your query is re writed as below:
list<Contact> mylist = [SELECT Homeroom__c ,Phone  ,Number_of_calls__c ,
(SELECT id FROM BroadBandCustomers__r ORDER BY HSI_Stage_Date__c)
 FROM Contact  ORDER BY LastName, FirstName]
 
<apex:repeat value="{!mylist}" var="con" >

    <apex:repeat value="{!con.BroadBandCustomers__r}" var="temp" >
       <apex:outputField value="{!temp.FieldName}" />
       ....
    </apex:repeat>

</apex:repeat>
Geoff DGeoff D
Tushar, this worked perfectly for me. Thank you.
Tushar Mahadik 13Tushar Mahadik 13
Thanks Geoff!!!