0

Alright guys, I have what I feel like is a simple problem. 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.

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>

1 Answer 1

1

You've designed the code in a way that only allows one Order Item per Order. This is because when you put a value in to a Map, if there's already an existing value, it gets bumped out. Instead, you'd want to prefer using a List inside the Map. Here would be a more appropriate fix to your code:

public class PaymentGroupController {
  public Map<Id, Orders__c> orderMap { get; set; }
  public Map<Id, List<Order_Item__c>> itemMap { get; set; }
  public PaymentGroupController() {
    // Initialize your variables
    orderMap = new Map<Id, Orders__c>();
    itemMap = new Map<Id, List<Order_Item__c>>();
    // Get all order records for the payment group
    for(Orders__c record: [SELECT Name FROM Orders__c WHERE Payment_Group__c = :ApexPages.currentPage().getParameters().get('id')]) {
      orderMap.put(record.Id, record);
      itemMap.put(record.Id, new List<Order_Item__c>());
    }
    // Get all order item records for those orders
    for(Order_Item__c record: [SELECT Taradel_Orders__c, Job_Name__c FROM Order_Item__c WHERE Taradel_Orders__c = :orderMap.keySet()) {
      itemMap.get(record.Taradel_Orders__c).add(record);
    }
  }
}

However, this is only meant to be meant as an answer that fixes the major problems in your code. A more elegant solution would be a simple query:

public class PaymentGroupController {
  List<Orders__c> orders { get; set; }
  public PaymentGroupController() {
    orders = [SELECT Name, (SELECT Job_Name__c from Order_Lines__r)
              FROM Orders__c WHERE Payment_Group__c = :ApexPages.currentPage().getParameters().get('id');
  }
}

Which reduces your Visualforce page's complexity drastically:

<apex:page controller="PaymentGroupController" docType="html-5.0" title="Your Invoice" id="YourInvoice" sidebar="false" cache="false" showHeader="false" standardStylesheets="true">
  <apex:repeat value="{!orders}" var="orderRecord">
    <Apex:outputText value="{!orderRecord.name}"/> <br></br>
    <apex:repeat value="{!orderRecord.Order_Items__r}" var="item">     
      <Apex:outputText value="{!item.Job_Name__c}"/><br></br> 
    </apex:repeat>
  </apex:repeat>
</apex:page>

Note: For the last set of code, the relationship was presumed to be Order_Items__r. You will need to go to the Order Item object and check the relationship field's "Child Relationship Name" to determine the correct name for the relationship.

1
  • You're second solution was definitely the best way to do it and solved my problem perfectly and thanks for your help! Mar 29, 2019 at 2:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .