function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh SRakesh S 

while developing apex class, i got problem ' Loop variable must be an SObject or list of contact '.

Dear all,
             i am writing a class to display contacts of standard objects(Contact). but while writing i got above problem. so could you pls rectify the problem? suggest me how to write program? what is the need of inner class? is it compulsory to write in every class.
this is my VF Page:
<apex:page standardController="Contact">
    <apex:form >
        <apex:pageBlock title="All Contacts">
            <apex:pageBlockSection >
                <apex:dataTable value="{!contact}" var="con" border="2" cellpadding="10">
                    <apex:column headerValue="Contact Name">
                        <apex:outputField value="{!con.lastname}"/></apex:column>
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!con.name}"/></apex:column>
                    <apex:column headerValue="Lead Source">
                        <apex:outputField value="{!con.LeadSource}"/></apex:column>
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!con.Phone}"/></apex:column>
                    <apex:column headerValue="Fax">
                        <apex:outputField value="{!con.Fax}"/></apex:column>
                    </apex:dataTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
this is my controller:
public class Contact
{
    List<Contact> conlist = new List<Contact>();
    public List<Contact> displayContacts(){
        for(Contact c:[SELECT lastname,name,leadsource,phone,fax FROM Contact]){
            conlist.add(c);
        }
        return conlist;
    }
    
    public class contactwrapper
    {
        private Contact con{get;set;}
        public contactwrapper(Contact c){
            con = c;
        }
    }
}


 
Best Answer chosen by Rakesh S
Virendra ChouhanVirendra Chouhan
Hi Rakesh,

In the above code you are using a standardController = 'Contact' in vf page.
and no extension or custom controller.

If you are using any customController or extension then you need to code in apex.

here if you want only show all the records of Contact object then you can do this with StandardController and recordSetVar attribute.
<apex:page standardController="Contact" recordSetVar="Contacts">
    <apex:form >
        <apex:pageBlock title="All Contacts">
            <apex:pageBlockSection >
                <apex:dataTable value="{!Contacts}" var="con" border="2" cellpadding="10">
                    <apex:column headerValue="Contact Name">
                        <apex:outputField value="{!con.lastname}"/></apex:column>
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!con.name}"/></apex:column>
                    <apex:column headerValue="Lead Source">
                        <apex:outputField value="{!con.LeadSource}"/></apex:column>
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!con.Phone}"/></apex:column>
                    <apex:column headerValue="Fax">
                        <apex:outputField value="{!con.Fax}"/></apex:column>
                    </apex:dataTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

RecordSetVar is a list which have all the records of the sObject which we use in standardController.

And about Inner class ther is no need to write in all the class .

Thanks & Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)

All Answers

SFDC-NOOBSFDC-NOOB
To get your code to work, you need to make a few changes.

See the code below. . . .


<apex:page Controller="Contactcontroller">
    <apex:form >
        <apex:pageBlock title="All Contacts">
            <apex:pageBlockSection >
                <apex:dataTable value="{!displayContacts}" var="con" border="2" cellpadding="10">
                    <apex:column headerValue="Contact Name">
                        <apex:outputField value="{!con.lastname}"/></apex:column>
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!con.name}"/></apex:column>
                    <apex:column headerValue="Lead Source">
                        <apex:outputField value="{!con.LeadSource}"/></apex:column>
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!con.Phone}"/></apex:column>
                    <apex:column headerValue="Fax">
                        <apex:outputField value="{!con.Fax}"/></apex:column>
                    </apex:dataTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



public class Contactcontroller
{
    List<Contact> conlist = new List<Contact>();
    public List<Contact> getdisplayContacts(){
        for(Contact c:[SELECT lastname,name,leadsource,phone,fax FROM Contact]){
            conlist.add(c);
        }
        return conlist;
    }
}
Virendra ChouhanVirendra Chouhan
Hi Rakesh,

In the above code you are using a standardController = 'Contact' in vf page.
and no extension or custom controller.

If you are using any customController or extension then you need to code in apex.

here if you want only show all the records of Contact object then you can do this with StandardController and recordSetVar attribute.
<apex:page standardController="Contact" recordSetVar="Contacts">
    <apex:form >
        <apex:pageBlock title="All Contacts">
            <apex:pageBlockSection >
                <apex:dataTable value="{!Contacts}" var="con" border="2" cellpadding="10">
                    <apex:column headerValue="Contact Name">
                        <apex:outputField value="{!con.lastname}"/></apex:column>
                    <apex:column headerValue="Account Name">
                        <apex:outputField value="{!con.name}"/></apex:column>
                    <apex:column headerValue="Lead Source">
                        <apex:outputField value="{!con.LeadSource}"/></apex:column>
                    <apex:column headerValue="Phone">
                        <apex:outputField value="{!con.Phone}"/></apex:column>
                    <apex:column headerValue="Fax">
                        <apex:outputField value="{!con.Fax}"/></apex:column>
                    </apex:dataTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

RecordSetVar is a list which have all the records of the sObject which we use in standardController.

And about Inner class ther is no need to write in all the class .

Thanks & Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
This was selected as the best answer
CJWilderCJWilder
If your just trying to get a list of all contacts you can change your vf page apex:page line to the below. You will not need the class you created with that change.

<apex:page standardController="Contact" recordSetVar="contact">

If your wanting todo something more complex list link may help.
https://developer.salesforce.com/page/Wrapper_Class