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
Max SmithMax Smith 

How to iterate over an sObject in Visualforce

Hi All,

Please see my code below
<table class="section">
        <tr>
            <apex:repeat value="{!hostedHeaders}" var="header">
                <th><apex:outputText value="{!header}"></apex:outputText> </th>
            </apex:repeat>   
        </tr>       
        <apex:repeat value="{!hostedProducts}" var="hProd">
        	<tr>
	       		<apex:repeat value="{!hProd}" var="prod">
	       			<td><apex:outputText value="{!prod}"></apex:outputText> </td>
	           	</apex:repeat>  
            </tr>
        </apex:repeat>
    </table>

I'm trying to dynamically create a table for an email template.  The table will have a dynamic number of columns.  The above visualforce markup is controlled by a custom controller on the back end and I can provide it if needed but I don't want to overly complicate things.

HostedProducts is a list of OpportunityLineItem.  The controller decides which OLI fields are queried and sent back to the component.  I want to display every queried field for each OLI returned.  Because this is dynamic, I don't know the field names until runtime.  So I can't use the typical {!hprod.unitprice}.

Any direction would be appreciated.

Thanks,
Max
Best Answer chosen by Max Smith
William TranWilliam Tran
use this syntax

{!header[fieldVar1]}

{!prod[fieldVar2]}

where fieldVar = dynamic field variable

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

All Answers

William TranWilliam Tran
use this syntax

{!header[fieldVar1]}

{!prod[fieldVar2]}

where fieldVar = dynamic field variable

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Max,

In order to achieve this you have to define a property as List of String in your controller class which will hold the name of query fields.
For Eg : If your query is : [Select Id,Name,UnitPrice,Amount from OLI] than this list will be containing four elements as : Id,Name,UnitPrice,Amount.
If your query is modified by either the addition of an extra field or the removal of any field than you have to maintain this list accordingly.

After your list is populated with correct elements than you can use it in your VF page as follows :
<table class="section">
        <tr>
            <apex:repeat value="{!hostedHeaders}" var="header">
                <th><apex:outputText value="{!header}"></apex:outputText> </th>
            </apex:repeat>   
        </tr>       
        <apex:repeat value="{!hostedProducts}" var="hProd">
        	<tr>
	       		<apex:repeat value="{!listOfFieldNames}" var="fieldName">
	       			<td><apex:outputText value="{!hProd.fieldName}"></apex:outputText> </td>
	           	</apex:repeat>  
            </tr>
        </apex:repeat>
    </table>
//listOfFieldNames is the name of list which you have created in Controller class

Below are the two lines from above code that are modified by me :
<apex:repeat value="{!listOfFieldNames}" var="fieldName"> <td>
<apex:outputText value="{!hProd.fieldName}"></apex:outputText> </td>


Right now this is the only possible way to show colums value dynamically.
Please let me know if anything is not clear to you or you need any help on this.

Thanks,
Abhishek.
 
Max SmithMax Smith
William and Abhishek, thank you both for your solutions.  Unfortunately I was traveling so I wasn't able to try them until now.

William, your solution worked like a charm.

Abhishek, for some reason your solution tries to reference the variable name instead of the variable value.  I get an error similar to "hProd.fieldName does not exist".  Im not sure is this is a problem with the API version I have or not. 

Best,
Max