Search code examples
salesforceapexvisualforce

Using Apex Repeat to Populate a Visualforce Table


It appears that my List is blank, not being passed through with the controller extension or otherwise some problem is causing my table not to populate the anticipated data. This visualforce page is being loaded from a custom button on a Work Order.

I've tried populating the data by using: :ApexPages.CurrentPage().getParameters().get('id')

As well as just hardcoding what I believe is the 'id' as shown in the code example.

The VF Page is as follows:

<apex:page Standardcontroller="WorkOrder" extensions="cWOInvoice1" renderAs="pdf">
    <apex:form >

...

              <table style="...">
                  <tbody style="...">
                  <tr style="display:table-row;">
                      <th style="...">Scope of Work</th>
                      <th style="...">Trade</th>
                      <th style="...">Price</th>                       
                      </tr>
                      <apex:repeat value="{!woli}" var="woli">
                      <tr>
                          <td style = "...">{!woli.Repair_Product__r.Name}</td>
                          <td style = "...">{!woli.Repair_Product__r.Trade__c}</td>
                          <td style = "...">${!woli.Item_Price_Roll_Up_Sub__c}</td>

                          </tr>
                      </apex:repeat>

                  </tbody>
              </table>
...
        </body>

    </apex:form>

</apex:page>

The controller looks like this (Again hardcoded workOrderID for bugtesting):

public class cWOInvoice1 {
    public WorkOrderLineItem woli {get;set;}
    public cWOInvoice1(ApexPages.StandardController controller){
        List<WorkOrderLineItem>woli=[SELECT Id, Area__c, Tenant_Responsible__c, WorkOrderId, Repair_Product__r.Name, Repair_Product__r.Bill_Rate_Retail__c, Repair_Product__r.Bill_Rate__c, Repair_Product__r.Trade__c FROM WorkOrderLineItem WHERE WorkOrderId='0WO55000000Cw4LGAS'];
    }
}

I expected the custom controller to pass through a list of work order line items that apex repeat would then display in the table. Instead, I get an empty table.


Solution

  • You have the variable woli declared with two different types in different scopes.

    At the controller level, woli is a single WorkOrderLineItem member variable:

    public WorkOrderLineItem woli {get;set;}
    

    Because it's never initialized, its value is null.

    Inside your constructor, you re-declare woli as a List<WorkOrderLineItem> and initialize it:

    public cWOInvoice1(ApexPages.StandardController controller){
        List<WorkOrderLineItem>woli=[SELECT Id, Area__c, Tenant_Responsible__c, WorkOrderId, Repair_Product__r.Name, Repair_Product__r.Bill_Rate_Retail__c, Repair_Product__r.Bill_Rate__c, Repair_Product__r.Trade__c FROM WorkOrderLineItem WHERE WorkOrderId='0WO55000000Cw4LGAS'];
    }
    

    This value goes out of scope at the end of the constructor and is never made available to your page.

    You need to remove the local declaration and correct the type of the member variable.

    Whether or not your <apex:repeat> currently compiles (I'm not sure if that's legal or not off the top of my head), you should change your iteration variable to have a different name than the bound property:

    <apex:repeat value="{!woli}" var="woli">
    

    You may wish to make the controller property something like woliList to clarify.