Example of Apex page code using iterative mapping in Salesforce

In Solution 2, I used a page and controller with version 25.0 to successfully implement an example where ‘Gender’ and ‘Type’ were used as keys in a map named ‘results’. The objective was to loop through the fields of a Salesforce object in Apex.
In Solution 1, it was found that displaying a complex type in one of the inner maps was not possible in a Visualforce page using a direct approach. This was due to the fact that a map of string,string was used as a key in the second map, which cannot be sorted. Using a map as a key in another map was not considered the correct approach since the keys cannot be sorted.


After attempting to recreate your issue, I experimented with various methods but was unsuccessful using your approach (confirmed by a visualforce error) and also had no success with


. Ultimately, I created a duplicate map with matching keys that simply stores the list size as an Integer, and retrieved the values from there.

Here is my code:

Page designed using VisualForce.


A controller for Apex.

public with sharing class TestController 
    public Map> testMap {get;set;}
    public Map listSizeMap {get;set;}
    public TestController()
        listSizeMap = new Map();
        testMap = new Map>();
        testMap.put('test1', new List{'1', '2'});
        testMap.put('test2', new List());
        for(String key : testMap.keySet())
            listSizeMap.put(key, testMap.get(key).size());

This explanation is about a map. Although it is an old answer, it might still be helpful for others.



on a map value is not possible.

In Apex, you can obtain a singular value from a map by using square brackets with the variable M, which is equivalent to



To determine the size of your map in Apex, you can utilize


, which is equivalent to



By utilizing the apex repeat, you can iterate through the keys in your map. The variable assigned to var=”M” denotes the current key within your loop. Every iteration of mapAllAttributesLabel[M] will retrieve the corresponding value for that key in your map.

Frequently Asked Questions

Posted in Uncategorized