1

I am working on one VF page where I need to show records based on text field like below

  1. Text Value1

    Record1
    Record2
    
  2. Text Value 2

    Record1
    Record2
    Record3
    

I created below class where I am storing text value and records in a map, can anyone help me out or guide me how to iterate over VF page to achieve above functionality

public class DeviationApprovalView {
Public Lease_2__c leases {get;set;} 
Public Lease_2__c acc {get;set;}
Map<string,List<Lease_Approval_History__c>> approvalMap {get; set;}
public DeviationApprovalView(){
    leases = [select Id,Name from Lease_2__c where Id =: ApexPages.currentPage().getParameters().get('id')];
    for(Lease_Approval_History__c cObj : [select Id,Name,Deviation__c,Lease__c from Lease_Approval_History__c
                                          where Lease__c =: leases.Id]){
                                              if(!approvalMap.keyset().contains(cObj.Deviation__c)){
                                                  List<Lease_Approval_History__c>clist;
                                                  clist.add(cObj);
                                                  approvalMap.put(cObj.Deviation__c, clist);
                                              }
                                              else{
                                                  List<Lease_Approval_History__c> cList;
                                                  cList = approvalMap.get(cObj.Deviation__c);
                                                  cList.add(cObj);
                                                  approvalMap.put(cObj.Deviation__c, cList);
                                              }
                                          }
}
}

VF Page:

<apex:page controller="DeviationApprovalView">
<apex:repeat value="{!approvalMap}" var="key">           
        <apex:repeat value="{!approvalMap[key]}" var="value">
            {!value} <br/>
        </apex:repeat>
</apex:repeat>
</apex:page>

I am getting error below on VF Page Unknown property 'DeviationApprovalView.approvalMap'

1 Answer 1

3

You can use apex:repeat to loop over the map on visualforce page.

<apex:repeat value="{!approvalMap}" var="key">           
            <apex:repeat value="{!approvalMap[key]}" var="value">
                {!value} <br/>
            </apex:repeat>
 </apex:repeat>

Requires a map with getter and setter in the controller as shown below

 public Map<string,List<Lease_Approval_History__c>> approvalMap {get; set;}

 public DeviationApprovalView(ApexPages.StandardController controller {
   acc = (Lease_2__c)controller.getRecord();
   leases = [select Id,Name from Lease_2__c where Id =: acc.Id];
   approvalMap = new Map<string,List<Lease_Approval_History__c>>();
   for(Lease_Approval_History__c cObj : [select Id,Name,Deviation__c,Lease__c from Lease_Approval_History__c
                                      where Lease__c =: leases.Id]){
                                          
      if(!approvalMap.keyset().contains(cObj.Deviation__c)) {
            List<Lease_Approval_History__c> clist = new List<Lease_Approval_History__c>();
            clist.add(cObj);                          
            approvalMap.put(cObj.Deviation__c, clist);
       }
     }
 }
6
  • Mohith, I am getting below error on VF page, I updated code in question - "Unknown property 'DeviationApprovalView.approvalMap'"
    – Kiran
    Commented May 23, 2021 at 10:34
  • Did you create a getter and setter for your map variable as I mentioned in my answer? It's important to do that! Commented May 23, 2021 at 10:36
  • yes, I have updated the code in question which I tried so far
    – Kiran
    Commented May 23, 2021 at 10:37
  • Your controller code does not have map as getter and setter there! Commented May 23, 2021 at 10:37
  • Map<string,List<Lease_Approval_History__c>> approvalMap {get; set;} I used this already in controller
    – Kiran
    Commented May 23, 2021 at 10:38

You must log in to answer this question.

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