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(){
= new map<string,integer>();
for(Account acc: [Select Id, Name, (Select Id, Name, Email from Contacts), Phone from Account]){
integer count
= data.get(;
if(count != null)
= 1;
data.put(, 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">
<apex:column headerValue="Duplicate Count">

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

Aslam Bari


Unknown 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. :)

Unknown 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.


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,

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...

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

Anonymous 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


Post a Comment