Play Games

Search This Blog

Monday, July 6, 2015

Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList

Apex Class :
public class SearchLeads {
    public Lead newLead  {get;set;}
    public list<lead> leadList  {get;set;}
    public SearchLeads () {
        newLead = new Lead();
    }
    public list<lead> doSearch() {
        leadList =[select id,name from lead where status =: newLead.status];
        return leadList ;
   }
}

Visual Force Page :
<apex:page controller="SearchLeads">
<apex:form>
Staus : <apex:inputField value="{!newLead.status}"/>
<apex:commandButton value="Search" action="{!doSearch}"/>
</apex:form>

</apex:page>
When we run this vf page and click on "Search" button,we will get this error
"Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList" because the apex action method i.e DoSearch()
return type is list<lead> not pagereference as shown in image below

So, to avoid this error change return type from list<lead> to "Pagereference" as shown in the code below.
public class SearchLeads {
    public Lead newLead  {get;set;}
    public list<lead> leadList  {get;set;}
    public SearchLeads () {
        newLead = new Lead();
    }
    public pagereference  doSearch() {
        leadList =[select id,name,Status  from lead where status =: newLead.status];
        return null;
    }
}

2nd Solution :
We can also change doSearch() as shown below.
public list<lead> doSearch() {
        leadList =[select id,name from lead where status =: newLead.status];
        return null;
}
Now run this page,you won't get any error.
Enjoy Coding...