Find your content:

Search form

You are here

Apex Repeat Using Set Collection

 
Share

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.


Attribution to: DCBoy

Possible Suggestion/Solution #1

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>

Attribution to: Peter Knolle

Possible Suggestion/Solution #2

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>

Attribution to: Scott Pelak
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/540

My Block Status

My Block Content