You need to sign in to do that
Don't have an account?
Jake Bullard
Apex:Repeat with a Apex:Repeat
Alright guys, I have what I feel like is a simple question. I want to show a map within a related map. I have written a controller and page to do that but for some reason the submap doesn't repeat. I only get the first record on the sub map.
Controller
public class PaymentGroupController { List<Orders__c> OrderList = [select Id, Name WHERE Payment_Group__c = :ApexPages.currentPage().getParameters().get('id')]; Map<Id, Orders__c> orderMap = new Map<Id, Orders__c>(OrderList); Map<Id, Order_Item__c> itemMap = new Map<Id, Order_Item__c>(); public PaymentGroupController(){ for(Orders__c o:OrderList){ string oId = o.Id; List<Order_Item__c> itemList = [Select Job_Name__C where Taradel_Orders__c=:o.id]; for(Order_Item__c i:itemList){ itemMap.put(oId, i ); } } } public Map<Id, Orders__c> getOrderMap(){ return orderMap; } public Map<Id, Order_Item__c> getitemMap(){ return itemMap; } }
Visualforce:
<apex:page controller="PaymentGroupController" docType="html-5.0" title="Your Invoice" id="YourInvoice" sidebar="false" cache="false" showHeader="false" standardStylesheets="true"> <apex:repeat value="{!orderMap}" var="key"> <apex:repeat value="{!orderMap[key]}" var="map"> <!--this should repeat all the Order__c found and it does--> <Apex:outputText value="{!map.name}"/> <br></br> <apex:repeat value="{!itemMap[map]}" var="submap"> <!--this should repeat all the Order_Items__c found under the Orders_c repeated above and it only outputs the first item in the map--> <Apex:outputText value="{!submap.Job_Name__c}"/><br></br> </apex:repeat> </apex:repeat> </apex:repeat> </apex:page>
is a map which holds only one order item for a key. Hence, it displays one item. If it is a Map<Id, List<Order_Item__c>> itemMap = new Map<Id, List<Order_Item__c>>(), it does make sense to apex:repeat the items for a particular key.
There are improvement areas in your code. We should never add soql inside a loop. It could hit the governer limit. Code should always be bulkified.
https://developer.salesforce.com/page/Apex_Code_Best_Practices
Looks like here the requirement is simple. So, handling in subquery itself without a map. Please check code below.