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
Kapil_KhandelwalKapil_Khandelwal 

Error in SOSL "find" query

When I run following query in Query Editor of Developer Console, I get the expected output.
 
FIND {old} IN ALL FIELDS RETURNING Shala, Id, Contact(FirstName)
But when I run same query in my Apex code, I get an error.
 
List<List<SObject>> oldList = [FIND {old} IN ALL FIELDS RETURNING Shala, Id, Contact(FirstName)];

Kindly help me regarding the same.


Thanks in advance.

 
SandhyaSandhya (Salesforce Developers) 
Hi,

Try to create declare old as String variable and then pass it.

Refer below sample code.

Below is the sample code that has a input text box and a command button which will search for the entered string in three Object . i.e Accounts, Contacts, Opportunities and returned result will be shown in the page block tables.
<apex:page controller="SOSLController">
  <apex:form >
  <apex:inputText value="{!searchStr}"/>
    <apex:commandButton value="Search in Account, Contact, Opportunity" action="{!soslDemo_method}"                reRender="acct,error,oppt,cont" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
                <apex:facet name="start" >
                    <img src="/img/loading.gif"/>                    
                </apex:facet>
    </apex:actionStatus>
  </apex:form>
 
    <apex:outputPanel title="" id="error">
     <apex:pageMessages ></apex:pageMessages>
     </apex:outputPanel>
 
    <apex:pageBlock title="Accounts" id="acct">
    <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 
 <apex:pageBlock title="Contacts" id="cont">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  
 <apex:pageBlock title="Opportunities" id="oppt">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
   
</apex:page>
 
Public with sharing class SOSLController{
 Public List<Opportunity> optyList {get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
  
 Public String searchStr{get;set;}
   Public SOSLController(){
   }
  
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   conList = New List<contact>();
   accList = New List<account>();
   if(searchStr.length() > 1){
   String searchStr1 = '*'+searchStr+'*';
   String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName)';
   List<List <sObject>> searchList = search.query(searchQuery);
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   if(accList.size() == 0 && conList.size() == 0 && optyList.size() == 0){
       apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..'));
       return;
   }
   }
   else{
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least two characters..'));
   return;
   }
  }
}

Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
sumithasumitha
Hi,
Try this
List<List<SObject>> oldList = [FIND :('*' + searchText + '*') IN ALL FIELDS RETURNING Shala, Id, Contact(FirstName)];

Hope it helps,

Thanks
Sumitha P
Shubham NandwanaShubham Nandwana
Hi Kapil,
In query editor we write the string to be matched as {stringToBeMatched} and in apex code, we use ( ' ) to specify the string to be matched.
So you can write your query as:
List<List<SObject>> oldList = [FIND 'old' IN ALL FIELDS RETURNING Shala, Id, Contact(FirstName)];

to avoid error.
For more information regarding how to write SOSL queries visit https://trailhead.salesforce.com/en/modules/apex_database/units/apex_database_sosl 
Mark this as best answer if it helps.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
Akshay_DhimanAkshay_Dhiman
Hi Kapil,

We have to use- ' at the start of string and end in Apex code 
Try this code

List<List<SObject>> searchList = [FIND 'old' IN ALL FIELDS 
                                      RETURNING Account(Name), Contact(FirstName,LastName)];
system.debug('--->'+searchList);

proper syntax used for sosl :

FIND {SearchQuery} 
[ IN SearchGroup ]
[ RETURNING FieldSpec [[ toLabel(fields)] [convertCurrency(***Amount***)] [FORMAT()]] ]
[ WITH DivisionFilter ]
[ WITH DATA CATEGORY DataCategorySpec ]
[ WITH SNIPPET[(target_length=n)] ]
[ WITH NETWORK NetworkIdSpec ]
[ WITH PricebookId ]
[ WITH METADATA ]
[ LIMIT n ]

[ UPDATE [TRACKING], [VIEWSTAT] ]


for more information go to below link:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm

Mark as best answer if it helps you

Thank You