4

My Controller returns a list of strings in a SET variable to Visual force page. I want to use an apex repeat to iterate over the SET collection but when I save the VF page it gives me an error. Is that we cannot use SET in apex:repeat?

This is the error message Wrong type for attribute. Expected String, found SetValue

The reason I dont want to use a LIST collection is because I want to remove duplicates in the strings which is why I have used SET.

  • 7
    You can easily convert the set to a list: List<String> stringList = new List<String>(stringSet); – metadaddy Aug 17 '12 at 1:25
  • 1
    Although Peter gave you the code to properly display a set within Visualforce, you may try posting relevant sections of your code in the future to help get to the root of the issue. In this case, your code is clearly not written correctly, and you may want to know exactly how it's not written correctly to avoid making the same mistake in the future. – Adam Aug 17 '12 at 15:54
14

You can definitely use a Set in a repeat. Maybe you have the Set in the wrong place or aren't using the {!} notation correctly? Double check with the reference on apex:repeat.

In the Controller. The property and initializing it in the Constructor, for example's sake:

public with sharing class TestController {
    public Set<String> aset { get; set; }

    public TestController () {
        aset = new Set<String>{'a','b','c'};
    }
}

The Visualforce snippet:

<apex:repeat value="{!aset}" var="a">
    <apex:outputText value="{!a}"/><br/>
</apex:repeat>
4

I know this is a bit late, but I ran into a similar issue:

<apex:page controller="Con_ShowEmSchweetly">
    <apex:repeat value="{!myIdSet}" var="myId">
        <c:mySchweetComponent relatedId="{!myId}"/>
    </apex:repeat>
</apex:page>

with my controller

public class Con_ShowEmSchweetly{
    public set<id> myIdSet {get;set;}

    public Con_ShowEmSchweetly(){
        this.myIdSet=new set<id>();
        for(Project__c p:[SELECT id FROM Project__c])
            myIdSet.add(p.id);
    }
}

I was getting this error:

Error: Wrong type for attribute <c:mySchweetComponent projectId="{!pId}">. Expected id, found SetValue

The solution is to change myIdSet to a list<id>.


Sample Component

Component Controller

public class Con_mySchweetComponent{
    public id projId {get;set;}
    public list<Project_Item__c> projectItemList {get;set;}

    public Con_mySchweetComponent(){
        this.projectItemList=new list<Project_Item__c>();
    }// NOTE: a component doesn't initialize variables, the component sets them after the controller is initialized

    public Project__c getProject(){
        if(projId==null)
            return new Project___c();

        // in this example, calling getProject() will initialize this.projectItemList
        this.projectItemList=[SELECT id,Name,Description__c
                             FROM Project_Item__c
                             WHERE Project__c =:projId];
        return [SELECT id,Name
                FROM Project__c
                WHERE id =:projId];
    }        

}//END Con_mySchweetComponent

Visualforce Component

<apex:component controller="Con_mySchweetComponent">
    <apex:attribute name="projectId" assignTo="{!projId}" 
                    type="id" required="true" 
                    description="Id for which Project to display"/>
    <head>
        <style>
            .displayNone{
                display:none;
            }
            .controllerCol-1{
                width:75%;
            }
            .controllerCol-2{
                width:25%;
            }
        </style>
    </head>
    <apex:pageBlock title="{!Project.Name}">
        <apex:pageBlockSection columns="2" title="Visualforce Pages">
            <apex:pageBlockTable value="{!ProjectItemList}" var="projectItem" columnClasses="controllerCol-1,controllerCol-2">
                <apex:column headerValue="Page"><apex:outputLink value="{!projectItem.id}">{!projectItem.Name}</apex:outputLink></apex:column>
                <apex:column headerValue="Description" value="{!projectItem.Description__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:component>
  • How does your component look like? – DCBoy Apr 7 '14 at 4:20
  • like you see it -- it sets the ProjectId as shown, and then the component gathers and displays records related to that Project – Scott Pelak Apr 7 '14 at 22:27
  • see the update =) – Scott Pelak Apr 7 '14 at 22:45

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.