+ Start a Discussion
Paul.FoxPaul.Fox 

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?

 

logontokartiklogontokartik
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
<pre>

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

</pre>
    
sunny.sfdcsunny.sfdc
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.