function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Visualforce and Map issue.

I'm wondering if anyone else has had this experience or knows why it won't work.


I know Map.values() returns a list of objects in an arbitrary order. However, what I didn't know was that Visualforce can't understand this order.


I had a visualforce page that was showing a list of Accounts and some objects inside of them. For many reasons I was storing the Accounts in a Map grouped by their IDs and stored in a wrapper class.


Map<Id,AccountWrapper> AccountMap;


So in my getAccounts() Method I was just using this at the end:

return AccountMap.values();


But I was getting some very strange behavior when users made changes to the Account Wrapper and saved it.


Turns out sometimes it would save Account 1 data into the Account 2 wrapper object. 


I switched the end of the getAccounts method to the following:


AccountWrapper[] AccountList = AccountMap.values();
return AccountList;

 Now the AccountList always has the right values.


Does anyone know why maps cannot be sent directly to the page? Why can't Salesforce update map values?


The values() method when used would always return the list in an arbitrary order, so the list is not guanranteed and when its saved, yeah, it gets saved sporadically.

The right way to use maps in VF pages is to use <apex:repeat>  to repeat over the keys and get the values. i.e

   <apex:repeat value="{!accountMap}" var="key">
       <apex:outputText value="{!accountMap[key].Account.Name}"/><!--Something like this>

I believe rather than using AccountMap.values() everytime to get the values you should refer to the same instance of AccountMap which you are using in visualforce page. Everytime you call values() it gives you the list in arbitrary order.