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
sonali  vermasonali verma 

error in sosl query

Hello friends
I have search page, I enter account name and it will fetch relevant fields
I have given code in Visual force page and apex class
<apex:page standardController="Account"
           extensions="MySearch">
           tabStyle="Account">
          
 <apex:form>
 
       <apex:inputText value="{!searchstring}"/>
      
       <apex:commandButton value="{!SearchAccount}"
                           title="Account Search}"/>
                          
       <apex:pageBlock title="Account Info">
      
           <apex:pageBlockTable value="{!accts}"
                                var="a">
                               
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
               <apex:column value="{!a.Industry}"/>          
          
           </apex:pageBlockTable>
      
       </apex:pageBlock>
 
 </apex:form>
  </apex:page>

public class MySearch
{
    list<Account> accts;
    public String searchstring{get;set;}
    public MySearch(ApexPages.StandardController controller)
    {
    }
    public list<Account> getaccts()
    {
       return accts;
    }
      
    public PageReference SearchAccount()
    {
       accts = (list<Account>) [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];
       return null;
    }
   
}
​​
when I compile apex class I am getting an error as : "Compile Error: Incompatible types since an instance of List<List<SObject>> is never an instance of List<Account> at line 18 column 16​

Please let me know how I can resolve this​

thanks
sonali
 
Best Answer chosen by sonali verma
Le NguyenLe Nguyen
I do not meet any problem when I tried to execute it.
 
public class MySearch
{
    list<Account> accts;
    public String searchstring{get;set;}
    public MySearch(ApexPages.StandardController controller)
    {
    }
    public list<Account> getaccts()
    {
       return accts;
    }
      
    public PageReference SearchAccount()
    {
      // accts = (list<Account>) [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

     List<List<SObject>> lstRTR = [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

    accts =(list<account>)lstRTR[0];
       return null;
    }
   
}
 
<apex:page standardController="Account"
           extensions="MySearch">
          
  <apex:form>
 
  <apex:inputText value="{!searchstring}"/>
     
       <apex:commandButton action="{!SearchAccount}"
                          value="Account Search}"/>
                         
       <apex:pageBlock title="Account Info">
     
           <apex:pageBlockTable value="{!accts}"
                                var="a">
                              
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
               <apex:column value="{!a.Industry}"/>         
         
           </apex:pageBlockTable>
     
       </apex:pageBlock>
  </apex:form>
  </apex:page>

User-added image

Le

All Answers

Le NguyenLe Nguyen
HI Sonali,

Because with SOSL, the return will be List<List<SObject>>.  Basically, you can not cast this type into list<Account>.
 
public PageReference SearchAccount()
    {
      // accts = (list<Account>) [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

     List<List<SObject>> lstRTR = [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

    accts =(list<account>)lstRTR[0];
       return null;
    }

Le
Ajay K DubediAjay K Dubedi
Hi Sonali,
Since the SOSL query results in a list of lists. So each list contains an array of the returned records. So for that purpose you have to create a list of list of the Account and then assign the values in it.
Hence you can modify your code as below :-

<-----------controller---------->
public class MySearch
{
    public list<list<Account>> accts;
    public String searchstring{get;set;}
    
    public MySearch(ApexPages.StandardController controller)
    {
    }
    public list<list<Account>> getaccts()
    {
       return accts;
    }

    public PageReference find()
    {
       accts = [FIND : searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];
       return null;
    }
   
}
Thanks.
sonali  vermasonali verma
Hi ajay
I modified the apex code as given by you. It compiles fine but the visual force page throws an error as "Error: Unknown property 'VisualforceArrayList.Name'.

<apex:page standardController="Account"
           extensions="MySOSLSearch">
          
  <apex:form>
 
  <apex:inputText value="{!searchstring}"/>
     
       <apex:commandButton action="{!SearchAccount}"
                          value="Account Search}"/>
                         
       <apex:pageBlock title="Account Info">
     
           <apex:pageBlockTable value="{!accts}"
                                var="a">
                              
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
               <apex:column value="{!a.Industry}"/>         
         
           </apex:pageBlockTable>
     
       </apex:pageBlock>
  </apex:form>
  </apex:page>​​
 
Please let me know
sonali  vermasonali verma
Hi Le Nguyen
I modified the code as given by you works fine but VF page showing error as "Error: Unknown property 'VisualforceArrayList.Name'

could u pls let me know.

Thanks
sonali​​​
Le NguyenLe Nguyen
I do not meet any problem when I tried to execute it.
 
public class MySearch
{
    list<Account> accts;
    public String searchstring{get;set;}
    public MySearch(ApexPages.StandardController controller)
    {
    }
    public list<Account> getaccts()
    {
       return accts;
    }
      
    public PageReference SearchAccount()
    {
      // accts = (list<Account>) [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

     List<List<SObject>> lstRTR = [FIND :searchstring IN ALL FIELDS RETURNING Account(Name, Type, Industry)];

    accts =(list<account>)lstRTR[0];
       return null;
    }
   
}
 
<apex:page standardController="Account"
           extensions="MySearch">
          
  <apex:form>
 
  <apex:inputText value="{!searchstring}"/>
     
       <apex:commandButton action="{!SearchAccount}"
                          value="Account Search}"/>
                         
       <apex:pageBlock title="Account Info">
     
           <apex:pageBlockTable value="{!accts}"
                                var="a">
                              
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
               <apex:column value="{!a.Industry}"/>         
         
           </apex:pageBlockTable>
     
       </apex:pageBlock>
  </apex:form>
  </apex:page>

User-added image

Le
This was selected as the best answer
sonali  vermasonali verma
Hi Le
It works . Thanks

one more thing.
I need to have a custom popup which only display all account names. Then I will select the acocunt name and perform search criteria
could u just tell me how to go about if?
​​
​thanks
sonali verma​
Ajay K DubediAjay K Dubedi
Hii Sonali,

I have made custom popup on a custom object with the help of page reference. Which on preview shows the home page after which on button click it opens up new page along with your requriement for search criteria based on name and displays it to the home page once you select it. You can use it for your standard object with the reuired functionality.

<----------Controller----------->
public class custompopupcls {

    public boolean popvalue { get; set; }
    public String name {get;set;}
    public list <Electronic__c> elect {get;set;}
    public Id addele {get;set;}
    public String n2;
    public String searchitem {get;set;}
    
    public custompopupcls()
    {
        elect = new list <Electronic__c> ();
        elect = [select id,Name from Electronic__c];
    }

    public PageReference showpopup() {
        // popvalue=true;
        PageReference pr= new PageReference ('/apex/custompopuprefer');
        pr.setRedirect(true);
        return pr;
    }
    
    public void add() {
        Electronic__c el = new Electronic__c (id = addele);
    }
    
     public PageReference n1()
    {
        n2= ApexPages.CurrentPage().getParameters().get('nameadd');
        Electronic__c el1= new Electronic__c ();
        el1 = [select name from Electronic__c where id =: n2];
        name=el1.name;
        PageReference pr1 = new PageReference ('/apex/custompopuppagerefer');
        pr1.setRedirect(false);
        return pr1;
    }  
     public void display()
    {
        elect=new list <Electronic__c>();        
        elect=[select id,Name from Electronic__c where Name like : '%'+searchitem+'%'];
      }  
}
<----------Page1-------->
<apex:page controller="custompopupcls">
    <apex:form >
        <apex:pageBlock >
                <apex:inputText value="{!name}" label="Name"/>
                <apex:commandButton action="{!showpopup}" value="Display"/>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
<--------Page2-------->
<apex:page controller="custompopupcls">
    <apex:form >
        <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:inputText value="{!searchitem}" label="search box"/>
                <apex:commandButton action="{!display}" value="Search" style="margin:center;"/>
        </apex:pageBlockSection>
            <apex:inputText value="{!name}" label="Name"/>
            <apex:commandButton value="Add" action="{!add}">
                <apex:param name="add_id" value="" assignTo="{!addele}"/>
            </apex:commandButton>
                    
            <apex:pageBlockTable value="{!elect}" var="v">
                <apex:column >
                    <apex:commandLink value="{!v.name}" action="{!n1}">
                        <apex:param name="nameadd" value="{!v.Id}"/>
                    </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>
Thanks