2

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"/>
</apex:pageBlockTable>

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" />
</apex:pageBlockTable>
3
  • I suggest use rendered attribute of apex:column if mapZipLeadsCount[Zip.ZipCode] not null then display
    – Ratan Paul
    Commented Mar 22, 2016 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
    Commented Mar 22, 2016 at 6:42
  • the count should be displayed 0 if the key isn't present in the mapZipLeadsCount
    – sfdcFanBoy
    Commented Mar 22, 2016 at 7:08

1 Answer 1

1

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"/>
1
  • 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
    Commented Mar 22, 2016 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .