Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
 
Any reason why can't do this in controller? –  mast0r Aug 21 at 13:54
 
actually my pageblocktable will be in apex:repeat and each time keyset of map would be different(I have other conditions in controller which populate the map with varying no.of keys). so i cant do this in controller –  rani Aug 21 at 14:04
 
See my answer on how I resolved this problem. –  sfdcfox Aug 21 at 14:12

3 Answers

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.


EDIT

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.

share|improve this answer

You should really keep this type of logic out of a Visualforce page. A Visualforce page should be used simply for View logic. Instead, add it as part of your controller:

Apex

public Boolean getRenderPageBlockTable(){
    return mapToCheck.containsKey('keyToCheck');
}

Visualforce

<apex:pageBlockSection rendered="{!renderPageBlockTable}">
    ...
</apex:pageBlockSection>
share|improve this answer

You can build a delimited string with all the keys of the map in controller and then you use SF formula

CONTAINS(delimited_string_with_map_keys, key)

in a rendered condition display map values with the key.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.