1

I am trying to iterate inner map in visualforce

Public Map<String,Map<String,String>> planMetadataMap {get;set;}

This is my datatype

    <apex:repeat value="{!planMetadataMap}" var="planMain">
        <apex:repeat value="{!planMain}" var="mapSubPlan">
            <td>${!qliWrapper.planMap[mapSubPlan['Pharma']]}</td>
            <td>${!qliWrapper.planMap[mapSubPlan['Non-Pharma']]}</td>
        </apex:repeat>
    </apex:repeat>

I am trying to iterate it like, it's not letting me save, saying Expression of type Text cannot be subscripted.

The sample value in my map would be ---

<Plan_General_Name,<<Pharma,Plan_Specific_Name>,<Non-Pharma,Plan_Specific_Name>>>,

I want to get Plan_Specific_Name to get it from the qliWrapper.planMap[Plan_Specific_Name]

What's the solution on this?

Thanks

2

You will have to iterate over planMetadataMap in the first repeat to get the inner Map, the var for the first repeat would be the key of the outer map. Then using that key, access the inner map and then again iterate using the repeat tag wherein the var for the inner repeat would be the key of the inner map. Now, to access the values in the map, you should use planMetadataMap variable with both the keys. So your visualforce code would be

<apex:repeat value="{!planMetadataMap}" var="planMain">
    <apex:repeat value="{!planMetadataMap[planMain]}" var="mapSubPlan"> 
        <td>${!planMetadataMap[planMain][mapSubPlan]}</td>
    </apex:repeat> 
</apex:repeat>

With your sample data, this is how it will be, you can remove the keys and the line breaks that is just added for better understanding

<apex:repeat value="{!planMetadataMap}" var="planMain">
    {!planMain} <!--this is key of outer map i.e. Plan_General_Name--><br/> 
    <apex:repeat value="{!planMetadataMap[planMain]}" var="mapSubPlan"> 
        <br/>{!mapSubPlan} <!--this is key of inner map i.e. Pharma  or Non-Pharma--><br/>
        ${!planMetadataMap[planMain][mapSubPlan]} <!--this is the final value i.e. Plan_Specific_Name-->
    </apex:repeat> 
</apex:repeat>
|improve this answer|||||
  • 1
    The $ in this case is to show it's a dollar amount, not part of the merge field. – sfdcfox Sep 20 '19 at 12:54
  • 1
    Ahh I see, updated the answer. Thanks for pointing out. – Vijay Ganji Sep 20 '19 at 13:17
  • 1
    Worked. any idea if I can sort the inner map :D – mvk1991 Sep 20 '19 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.