can we check if a key exists in a map in vf page

is it possible to check for a specific key in a map i.e., whether it exists or not in VISUALFORCE page?

Based on this i need to render a pageblocktable.


No, there’s no way to tell if a key exists or not. We don’t have access to “containsKey”, and any attempt to reference a key that doesn’t exist will cause your code to crash and burn catastrophically.

Visualforce Error
Help for this Page

Map key 'value' not found in map
Error is in expression '{!not(isnull(keys[key]))}' in component <something> in page xyz

This is related to bug W-1065879 (according to an old forum post).

The solution is to check the key in Apex Code, and prevent the rendering of the element or component before the attempted access.

Here’s an example used in live code:

public with sharing class orderCreateController {

    public Map<Id,Boolean> selectId { get; set; }
    public Map<Id,Quote> quoteList  { get; set; }

    // Irrelevant code redacted here

    // FIX FOR BUG W-1065879
    public wrapper[] getwrapperlist() {
        wrapper[] items = new wrapper[0];
        if(quotelist != null) {
            for(id quoteid:quotelist.keyset()) {
                items.add(new wrapper(this,quoteid));
        return items;

    public class wrapper {
        public wrapper(orderCreateController c,id recordid) {
            con = c;
            id = recordid;
        public boolean selected { get { return con.selectid.get(id); } set { con.selectid.put(id,value); } }
        public quote quote { get { return con.quotelist.get(id); } }

        private id id;
        private orderCreateController con;

By iterating over {!wrapperlist}, we can create a pseudo-wrapper that will prevent non-existent keys from appearing.


I just realized that this code takes some explaining. You can select from a list of quotes. The map may not always be populated with boolean values, so this wrapper class protects the Visualforce map controller from croaking.

Source : Link , Question Author : rani , Answer Author : sfdcfox

Leave a Comment