I have an apex:pageblocktable in VF Page with 2 columns. The 2nd column is displayed from a map.

<apex:pageBlockTable value="{!noZips}" var="Zip">
<apex:column value="{!Zip.ZipCode}" headerValue="Name"/>
<apex:column value="{!mapZipLeadsCount[Zip.ZipCode]}" headerValue="Lead Count"/>

The above gives error, when the Map doesn't contain that zipCode. Can we use CONTAINS KEY in Visualforce page to check for the key existence? How?

Also, I tried to put the zipcodes in a list (zipList in the controller) and checked if the list CONTAINS the key (code below). Even this fails.

<apex:pageBlockTable value="{!noZips}" var="Zip">
<apex:column value="{!Zip.ZipCode}" headerValue="Name"/>

<apex:column value="{!IF(CONTAINS(zipList,Zip.ZipCode),mapZipLeadsCount[Zip.ZipCode],0)}" headerValue="Lead Count" />
  • I suggest use rendered attribute of apex:column if mapZipLeadsCount[Zip.ZipCode] not null then display – Ratan Paul Mar 22 '16 at 2:51
  • Logically, is it required on for the map 'mapZipLeadsCount' to have all the elements present in the list 'noZips' as keys? . One way to avoid this error would be to, filter out the elements from the list 'noZips' that are not present as keys of the map. – Prajith Mar 22 '16 at 6:42
  • the count should be displayed 0 if the key isn't present in the mapZipLeadsCount – sfdcFanBoy Mar 22 '16 at 7:08

I've hit this in Visualforce too and the only solution I've found is to ensure (in the controller) that the map does contain all the key values.

In your case before you populate the actual counts in the controller you could initialise the count for every ZipCode to zero:

for (Zip z : noZips) {
    mapZipLeadsCount.put(z.ZipCode, 0);

and then this Visualforce will just work unmodified:

<apex:column value="{!mapZipLeadsCount[Zip.ZipCode]}" headerValue="Lead Count"/>
  • Exactly! I did the other way though. Instead of populating all with 0, I populated only for those zips which do not exist in mapZipLeadsCount. – sfdcFanBoy Mar 22 '16 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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