+ Start a Discussion

Map in Visualforce results Incorrect parameter type for subscript. Expected Number, received Text

This is a bit of breif bit of code:


public List<String> partTypes = new List<String>{'Accessories', 'Electronics', 'Fishing', 'Fun and Entertainment', 'Graphics and Decals', 'Pontoon Covers', 'Safety', 'Seating', 'Trailering and Covers'};

 public Map<String, List<Product2>> availablePartOptions{ get; set;}
List<Product2> boatOptions = [SELECT Id, Name, RecordType.Name, Family,
                                                        (SELECT Id, Name, UnitPrice, Pricebook2Id
                                                          FROM PricebookEntries WHERE Pricebook2Id = :pb2Id),
                                                        (SELECT Id, Standard__c, Maximum__c FROM From_Product_Options__r)
                                                        FROM Product2
                                                        WHERE Id IN :ids];

availablePartOptions = new Map<String,List<Product2>>();
for(String partName : partTypes) {
   availablePartOptions.put( partName, new List<Product2>() );

for(Product2 opt: boatOptions() ) {
  if(opt.RecordType.Name == 'Part'){


Visualforce test:


    <apex:repeat value="{!availablePartOptions}" var="key">
              <apex:outputText value="{!key}" /> -
               <apex:outputText value="{!availablePartOptions[key].size}" />


This all results in:

Incorrect parameter type for subscript. Expected Number, received Text
Error is in expression '{!availablePartOptions[key].size}' in component <apex:outputText> in page boatbuilder

If I omit the offending line I get a ugly but valid list of keys which is the same as my partTypes List

Accessories -
Electronics -
Fishing -
Fun and Entertainment -
Graphics and Decals -
Pontoon Covers -
Safety -
Seating -
Trailering and Covers -

This is the method every doc I have read said to use when dealing with maps in visualforce.
If anyone has any Ideas it would be a tremendous help

I think I used size method with maps and it works. So what if you use apex:variable ?

<apex:variable value="{!availablePartOptions[key]}"  var="myMap"/  >
<apex:outputText value="{!myMap.size}" />

Does this works for you ?
Fahad Khan 2Fahad Khan 2
avidev9' Reply should be marked as answer
Michael KonykMichael Konyk
Avidev9, Thanks man.
Sorry guys for not responding.  Everything I posted in the original post works fine.  I t was a bug introduced by Salesforce on thier backend during a Java update which broke the Visualforce map handling ( briefly ).  I sent quite some time on the phone with Salesforce as this completely broke our main tool for selling anything.  I then simply forgot to update this question.  Avidev9 I am unsure if at the time if the apex:variable  would have resolved the issue, but all is functioning properly a while after I posted the question.  Thanks
Justin MitchellJustin Mitchell
Hi. I just stumbled across this thread. It seems that this is still broken or was never fixed(?) I'm getting the same error if I try to use 
on a Visualforce page. However, just in case anyone else sees this, the solution presented by Avidev9 does indeed work:
<apex:variable value="{!mapName[key]}" var="list" />
<apex:outputtext value="{!list.size}" />