5

In my controller I have a map of integers and strings which I want to display as checkboxes.

The controller:

public Map<Integer, String> xmlCatalogsMap {get;set;}
public Set<Integer> xmlCatalogsKeys {get;set;}

public void getXMLCatalogs() {
    String sql = 'SELECT BOID__c, Name FROM ZXMLCatalogs__c ORDER BY Name';
    List<ZXMLCatalogs__c> result = Database.query(sql);
    Map<Integer, String> xmlCatalogsMap = new Map<Integer, String>();

    for (ZXMLCatalogs__c c : result) {
        xmlCatalogsMap.put(Integer.valueOf(c.BOID__c), c.Name);
    }

    Set<Integer> xmlCatalogsKeys = xmlCatalogsMap.keySet();
}

getXMLCatalogs() is called from constructor.

Now I want to display a checkbox for each integer/string pair. Integer/key should be the checkbox value and the string should be the label:

<apex:repeat value="{!xmlCatalogsKeys}" var="i">
    <apex:pageBlockSectionItem>
        <apex:inputCheckbox label="{!xmlCatalogsMap[i]}" value="{!i}" html-name="CatalogId" />
    </apex:pageBlockSectionItem>
</apex:repeat>

I read how to display map on vf page in salesforce and How to reference a map's keySet() and individual map elements within Visualforce? Get 'Unknown function' error

When I compile with Sublime/Maven I always get the below error without line number:

Unknown property 'i'

Why can't I use i (or any other variable name) as a variable?

  • one error I can see Set<Integer> xmlCatalogsKeys = xmlCatalogsMap.keySet(); you should reinitialise the variable here. You have assign the values xmlCatalogsKeys = xmlCatalogsMap.keySet(); – Ratan Paul Apr 4 '17 at 13:29
  • Could you test with Set<Integer> xmlCatalogsKeys = new Set<Integer>(xmlCatalogsMap.keySet()); ? – Martin Lezer Apr 4 '17 at 13:39
  • I changed the assignment of xmlCatalogsKeys, but the problem remains. Other examples here use keySet and a variable too. – Obi_77 Apr 4 '17 at 13:48
  • Just tried the same conditions and it's working with other type of components. It looks like a bug with the apex:inputCheckbox – Martin Lezer Apr 4 '17 at 14:19
  • Please check my answer, it is working – Santanu Boral Apr 4 '17 at 16:09
2

In the line:

<apex:inputCheckbox label="{!xmlCatalogsMap[i]}" value="{!i}" html-name="CatalogId" />

value attribute should be Boolean. So that checkbox can be marked as check or uncheck.

Currently it's Integer. That's why it is showing error.

To fix this problem, set up a map of Integer to Boolean, then bind the checkbox to that instead.

public Map<Integer, Boolean> xmlCatalogsChecked { get; set; }

public void getXMLCatalogs() {
    // Query omitted
    xmlCatalogsChecked = new Map<Integer, Boolean>();
    for (ZXMLCatalogs__c c : result) {
        xmlCatalogsMap.put(Integer.valueOf(c.BOID__c), c.Name);
        // Assign default state
        xmlCatalogsChecked.put(Integer.valueOf(c.BOID__c), false);
    }
}

<apex:inputCheckbox label="{!xmlCatalogsMap[i]}" value="{!xmlCatalogsChecked[i]}" 
    html-name="CatalogId" />

Update based on Comments

Main issue in your code is this line at getXMLCatalogs() method which is initializes the map and makes that empty.

Map<Integer, String> xmlCatalogsMap = new Map<Integer, String>(); 

I have create apex class and pages just like you and it is working correctly.

Apex class

public class DealerController
{
    public Map<Integer, String> xmlCatalogsMap {get;set;}
    public Map<Integer, Boolean> xmlCatalogsChecked { get; set; }

    public Set<Integer> xmlCatalogsKeys {get;set;}

    public DealerController()
    {
        getXMLCatalogs();
    }

    public void getXMLCatalogs() {
        xmlCatalogsKeys = new Set<Integer>();

        String sql = 'SELECT Dealer_Number__c, Name FROM Dealer__c ORDER BY Name';
        List<Dealer__c> result = Database.query(sql);

        xmlCatalogsMap = new Map<Integer, String>();
        xmlCatalogsChecked = new Map<Integer, Boolean>();

        for (Dealer__c c : result) {
            xmlCatalogsMap.put(Integer.valueOf(c.Dealer_Number__c), c.Name);
            // Assign default state
            xmlCatalogsChecked.put(Integer.valueOf(c.Dealer_Number__c), false);
        }
         xmlCatalogsKeys = new Set<Integer>(xmlCatalogsMap.keySet());


    }

}

Visualforce

<apex:page id="dealerPage" controller="DealerController" action="{!getXMLCatalogs}">
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:repeat value="{!xmlCatalogsKeys}" var="i">


            <apex:inputCheckbox label="{!xmlCatalogsMap[i]}" value="{!xmlCatalogsChecked[i]}" 
            html-name="CatalogId" />


    </apex:repeat>
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Result checkbox

  • I tried the boolean map and value="{!xmlCatalogsChecked[i]}". No compile error, but checkboxes are not displayed. It's just a headache. – Obi_77 Apr 4 '17 at 15:02
  • Please check my ans, it is working as expected. – Santanu Boral Apr 4 '17 at 16:07
  • Ok, it seems that value attribute can only store boolean. Would be better for my case to store ID numbers, so I switched to a multi selectList, much easier. – Obi_77 Apr 5 '17 at 8:35
0

Why can't I use i (or any other variable name) as a variable?

I believe the issue is that what you want to do in your code is the following:

{!xmlCatalogsMap.get(xmlCatalogsKeys)}

You'd likely be better off doing that in your controller and creating a list of xmlCatalogMapValues.

  • Unfortunately the first method won't work, because we can't call methods with parameters that way in Visualforce. The second suggestion is definitely preferred unless there's a good reason for having the map in the first place. – sfdcfox Apr 4 '17 at 14:38
  • Exactly and that was the point I was trying to make. – crmprogdev Apr 4 '17 at 15:02

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.