Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a map exposed to my visualforce page and I want to loop over it and display key and value


public Map<String,String> myMap { get; set; }

//init myMap 


 <apex:repeat value="{!myMap.keySet() }" var="fieldKey">
    key: {!fieldKey }
    value: {!myMap.get(fieldKey ) }

but it gives the error Error: Unknown function myMap.keySet. Check spelling

share|improve this question
up vote 27 down vote accepted

The Visualforce for this situation is this:

<apex:repeat value="{!myMap}" var="fieldKey">
    key: {!fieldKey }
    value: {!myMap[fieldKey]}

because the map key is automatically used by the apex:repeat and square brackets are the expression language way of looking up a value in a map.


Most of the time the lack of ordering of the map keys isn't a good thing; iterating over a separate controller property that contains a sorted list of the key values instead is a way to address that:

public List<String> orderedKeys {
    get {
        List<String> keys = new List<String>(myMap.keySet());
        return keys;
share|improve this answer

It might be a lot simpler to create a List of wrapper classes and have the key/value in there.

public class MyWrapper
  public String key{get; private set;}
  public String value{get; private set;}

  public MyWrapper(String key, String value)
    this.key = key;
    this.value = value;

You can create the list of wrapper in your controller like:

myWrappers = new List<MyWrapper>();
for (String key: myMap.keySet())
  myWrappers.add(new MyWrapper(key, myMap.get(key));

You can then use repeat over this list you created.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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