0

While i'm displaying set data into vf page it's showing the error

vf code :--

<apex:page controller="Soql_Ct">
    <apex:form >
        <apex:pageBlock title="Soql_Example" >

            <apex:pageBlockTable value="{!accounts}" var="a">
                    <apex:column value="{!a.Id}"/>

            </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>

apex code:--

public class Soql_Ct {
    public Set<Account> accounts {set;get;}
    public List<Account> myList {set;get;}
    public Soql_Ct(){
        myList=[select Id,Name,Phone,Industry,Rating,Ownership from Account];
        accounts=new Set<Account>();
        accounts.addAll(myList);
    }    
}
1
  • Have you tried iterating it through myList instead of accounts.? Commented Jan 17, 2020 at 12:59

2 Answers 2

0

By default when iterating through Set<SObject> on visualforce page using any iterator, it returns as a String and not of Object type. In your case, a is a string type and not sobject. When you try to access the a.id it gives you an error.

Declare accounts as a List<Account> else use myList in your Visualforce page.

0

I have just made a simple change. Try out with this.Set will only include unique values.Suppose your account with 2 different id but same names. This will only take one value. Or just use the same myList to return

public class Soql_Ct {
public list<Account> accounts {set;get;}
public List<Account> myList {set;get;}
public Soql_Ct (){
    myList=[select Id,Name,Phone,Industry,Rating,Ownership from Account];
    accounts=new list<Account>();
    accounts.addAll(myList);
}    

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .