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
srinivas pulipatisrinivas pulipati 

Hi frnds i am writing apex program by using SOSL query but this program is correct and also writing vf page this also execute but when i click the button total records are not visible it throws exception please solve the exception?

Apex code:

public class SOSLProgramExample {
    public list<opportunity> optylist{set;get;}
    public list<lead> leadlist{set;get;}
    public list<account> actlist{set;get;}
    public list<contact> conlist{set;get;}
public SOSLProgramExample(){
    
}
public void soslDemo_method(){
  optylist=  new list<opportunity>();
    leadlist=new list<lead>();
    actlist =new list<account>();
    conlist =new list<contact>();
    list<list<sobject>> searchlist=[find 'test' in all fields returning account(id,name,type),contact(name,email),
                                   opportunity(name,stagename),lead(company,name,status)];
    actlist=((list<account>)searchlist[0]);
       conlist=((list<contact>)searchlist[1]);
       leadlist=((list<lead>)searchlist[2]);
       optylist=((list<opportunity>)searchlist[3]);
    
}
}

Vf code:
<apex:page controller="SOSLProgramExample">
    <apex:form>
      <apex:commandButton value="show records using sosl" action="{!sosldemo_method}"/>
        <apex:pageBlock title="accounts">
            <apex:pageBlockTable value="{!actlist}" var="acc">
                <apex:column value="{!acc.name}"/>
                <apex:column  value="{!acc.type}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:pageBlock title="contacts">
           <apex:pageBlockTable value="{!conlist}" var="con">
               <apex:column value="{!con.name}"/>
               <apex:column value="{!con.email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
       <apex:pageBlock title="leads">
            <apex:pageBlockTable value="{!leadlist}" var="lead">
                <apex:column value="{!lead.name}"/>
                <apex:column value="{!lead.company}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
            <apex:pageBlock title="opportunity">
                <apex:pageBlockTable value="{!optylist}" var="opp">
                    <apex:column value="{!opp.name}"/>
                    <apex:column value="{!opp.stagename}"/>
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Visualforce Error:
System.TypeException: Invalid conversion from runtime type List<Opportunity> to List<Lead>
Error is in expression '{!sosldemo_method}' in component <apex:commandButton> in page soslprogramexample: Class.SOSLProgramExample.soslDemo_method: line 18, column 1
Class.SOSLProgramExample.soslDemo_method: line 18, column 1


 
Alexander TsitsuraAlexander Tsitsura
Hi srinivas,

Your sosl query return list of 4 objects: account,contact, opportunity, lead. And on line 18 you try convert list of oportunity to list of lead

Try code below
public class SOSLProgramExample {
    public list<opportunity> optylist{set;get;}
    public list<lead> leadlist{set;get;}
    public list<account> actlist{set;get;}
    public list<contact> conlist{set;get;}
public SOSLProgramExample(){
    
}
public void soslDemo_method(){
  optylist=  new list<opportunity>();
    leadlist=new list<lead>();
    actlist =new list<account>();
    conlist =new list<contact>();
    list<list<sobject>> searchlist=[find 'test' in all fields returning account(id,name,type),contact(name,email),
                                   opportunity(name,stagename),lead(company,name,status)];
       actlist=((list<account>)searchlist[0]);
       conlist=((list<contact>)searchlist[1]);
       optylist=((list<opportunity>)searchlist[2]);
       leadlist=((list<lead>)searchlist[3]);
    
}
}
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
Navee RahulNavee Rahul
Hello Srinivas,

In searchlist you were trying to  Assign opportunity data to lead Variable and thats the
problem.

Thanks
D Naveen Rahul.