Using Map In Your VF Pages

Wednesday, March 16, 2011 by Aslam - The Alexendra
Hi All,
Most of you aware of this but i am sure most of you (like me) un aware about that we can use Map directly on VF pages. Atleast for me, it is a news :) . Anyway, i am going to show you how to use simple map on your VF page so that you can avoid Model class (If you making only because of thinking that VF does not support Map). Here I am going to show a simple program which counts duplicate accounts by name and shows on VF screen. Here Account Name is "key" of Map and the "value" part is count of duplicate account.





Class Code:
public class TestMapController{

public map<string,integer> data {get;set;}

public TestMapController(){
data
= new map<string,integer>();
for(Account acc: [Select Id, Name, (Select Id, Name, Email from Contacts), Phone from Account]){
integer count
= data.get(acc.name);
if(count != null)
count
++;
else
count
= 1;
data.put(acc.name, count);
}
}
}




VF Page Code:
<apex:page controller="TestMapController">
<apex:pageblock title="Map Usage On VF">
<apex:pageBlockTable value="{!data}" var="d">
<apex:column headerValue="Account Name">
{!d}
</apex:column>
<apex:column headerValue="Duplicate Count">
{!data[d]}
</apex:column>
</apex:pageBlockTable>
</apex:pageblock>
</apex:page>



Hope this will help new developers who struggle to manage and show their data model on pages.

Thanks
Aslam Bari

19 comments:

sripriya said...

Good one again Aslam...

Abdul Vahid said...

Very Nice Aslam :)

Manish said...

Very useful post ... it's save lots of effort to display data on VF page. we can directly user map on the page. :)

Reena said...

Very nice post .... Yeah I was also unaware of this..... Good going.

sandeep said...

thanks Sir for sharing this useful Stuff with all of us.

anshulawesome said...

This post also showed me some new...........................
technique..........Thanks a lot..........

Anonymous said...

Thnks sir... today i understood the way of using map and along with that i also understood the actual use of model class ...

Raj Jha said...

Sir again you have done typical work in a easy way.

Harshesh said...

I have faced lot of issues because i was not aware of how to use Map in VF. This post has helped me a lot. Nice one indeed. Keep it up and keep sharing such things.

dan m said...

thats great and all, and imma let you finish, but how do you display say 4 or 5 fields using map? can you data[d][1...n] or something?

Anonymous said...

It really help me, Thanks

JD said...

This was good one! Was struggling with it for a few moments. Thanks for posting this!

Gajendra Sharma said...

Nice post Aslam Bari,
It really help me.

Thanks..

Deep Patil said...

Hi Aslam,

i have some what similar requirement,

i am creating a page to save a pricing record. pricing record has multiple package record and for every package record there are multiple services record to be saved.

working on it from last few days, very badly stucked..

Following link will help you to understand it in detail,

http://boards.developerforce.com/t5/Apex-Code-Development/New-items-are-not-getting-added-to-the-list-which-is-the-part-of/td-p/456237

Calvin said...

The only problem I see is the SOQL query is within the for loop.

prit said...

This is awesome Sir!!! It works like wonder!!Thanks

prit said...

@Calvin
FYI, i guess the SOQL query isn't inside the for loop. The loop is iterated over the query result(which is a list).

dimmys said...

Hi, want to add that we can also use map in map on page. Year ago I was using this.

something like this - Map>

Dima Smirnov

Anonymous said...

Nice tutorial

Thanks.

Post a Comment