2

I have a custom controller that returns a map like this:

Map<String, String>

In my visualforce page, how do I access this map? Before the Winter '13 release, I could do this:

<apex:variable value="{!results[key]}" var="result"/>

However, this no longer works with the Winter '13 release. How do I do this in the latest release?

Edit: The VF page is at API version 24.0. The Controller is at 25.0.

This is in my Visualforce Page:

<c:Query_article_view>
    <apex:variable value="{!results[articleid]}" var="result"/>
        {!result}
</c:Query_article_view>

This is my component:

<apex:component controller="Article_View" access="global">


    <apex:variable value="{!results}" var="results"/>
    <apex:componentBody />
</apex:component>

This is my Apex Class:

global with sharing class Article_View {

    global Map<String, String> results {
        get {
            KnowledgeArticleViewStat result;
            List<KnowledgeArticleViewStat> results = [SELECT NormalizedScore, Id, ParentId FROM KnowledgeArticleViewStat WHERE Channel='Csp'];
            Map<String, String> article_views = new Map<String, String>();
            for (Integer i = 0; i < results.size(); i++) {
                 article_views.put(((String)results[i].get('ParentId')).substring(0,15) , String.valueOf(((Decimal)results[i].get('NormalizedScore')).round()));
            }
            return article_views;
        } 
        set; 
    }
}

When I try and save my Visualforce Page, I get this error: Error: Expression of type Text cannot be subscripted

  • Specifically what number API version is both the controller and VF? – joshbirk Oct 15 '12 at 21:20
  • @joshbirk I made the edit in my original post. VF: 24.0, Controller: 25.0 – Di Zou Oct 15 '12 at 21:26
  • Can you post some sample code that's broken? – ca_peterson Oct 15 '12 at 23:14
  • @ca_peterson I added some code in the edits. – Di Zou Oct 16 '12 at 13:53
1

It appears that the VF compiler thinks that variable inside the component is returning the key to the map instead of the results map. If you try this with

map <Integer,String> 

you get an error that an integer can't be subscripted.

Can you pass in the key as an attribute to the component? Ex:

component:

<apex:component controller="Article_View" access="global">
<apex:attribute name="key" description="key for results" type="String" required="false"/>

 <apex:variable value="{!results[key]}" var="result"/> 
<apex:componentBody />

page:

<c:Query_article_view key="{!articleid}">

    {!result}
</c:Query_article_view>
  • So I changed my Apex Class to return map <Integer,String> and I changed the component and Visualforce page like you wrote. "{!results}[key]" just displays the dictionary and then literally [key]. It doesn't actually try find the value of that key. So when I load my VF page, I get this: {...}[key] – Di Zou Oct 16 '12 at 15:17
  • that was a typo. it should have been {!results[key]} – Greg Grinberg Oct 16 '12 at 21:39
  • So Salesforce is trying to access the Map now. However, I am getting a Map key not found in map error. I printed out the key and the values in the Map, and the KnowledgeArticleViewStat.ParentId does not match up with the KnowledgeArticle.Id. For example, I will have a KnowledgeArticleViewStat.ParentId that is a key in the Map that is kA1W00000004CMJKA2. Then my KnowledgeArticle.Id that I am trying to use as a key to access the Map is kA1W00000004CMJ. The last 3 letters are cut off for all the KnowledgeArticle.Id. Any ideas about this? – Di Zou Oct 17 '12 at 14:01
  • This is a 15 vs. 18 character ID issue. Try converting KnowledgeArticle.ID as done here: sfdc.arrowpointe.com/2010/08/23/… – Greg Grinberg Oct 17 '12 at 15:58
  • That converts the ID in the controller. Is there a way to convert it in the VF page on Component? – Di Zou Oct 17 '12 at 21:08
0

I have a page and controller on version 25.0, and this example work for me where 'Gender' & Type are the keys in a map called results

public with sharing class Article_View {

public Map<String, String> results;

public Map<String, String> getresults () {
         results = new Map<String, String> ();
         results.put ('Gender', 'All');
         results.put ('Type', 'All');

         return results;
    } 

}

<apex:page controller="Article_View" >

<apex:variable value="{!results['Gender']}" var="result"/>
   Gender: {!result}

       <br/>

<apex:variable value="{!results['Type']}" var="type"/>
        Type: {!type}        

</apex:page>
  • I made some edits, can you take a look? Thanks! – Di Zou Oct 16 '12 at 13:52
  • BritishBoy - can you provide the Apex? As grigriforce pointed out, it looks like the weak link is the variable component itself. You can access the map and values via key in the manner you'd expect right from the page level, but it looks like the component is saying it is too many levels of abstractions for it. But with that theory, I'm not sure why BritishBoy's code is working. – joshbirk Oct 16 '12 at 18:39
  • I added a controller and page - but not sure how the component is being used in your example - though I did see this comment in the notes: Note:<apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined. – BritishBoyinDC Oct 16 '12 at 20:00

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.