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
mavsmavs 

help on SOSL

Hi

 

Not sure where i am doing wrong. Simple one.... I have a string to search

 

 

String searchText = 'Acme'; String wildcardSearchText='%'+ searchText + '%'; string searchquery = 'FIND \'+wildcardSearchText+ \'IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name )'; System.debug('searchquery is '+searchquery ); List<List<SObject>> searchList=search.query(searchquery);

 

 

And the debug log looks like ,.

 

Debug Log: searchquery is FIND '+wildcardSearchText+ 'IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name )

 

 

 

 

When it should have been, FIND *Acme* IN ALL FIELDS RETURNING Account

 

I also tried in the below way and no luck

 

 

String searchText = 'Acme*'; List<List<SObject>> searchList = [FIND searchText '*' IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name)]; Account[] accts= ((List<Account>)searchlist[0]); System.debug('accts result is : '+accts);

 

 

unexpected token: 'searchText'

 

Please advice

Best Answer chosen by Admin (Salesforce Developers) 
ShakilShakil

First query is having small change in escape character, as you missed opening and closing single quotes

 

it shud be as below

 

    String searchText = 'Acme';

 

String wildcardSearchText='%'+ searchText + '%';
        
string searchquery = 'FIND \'' + wildcardSearchText + '\' IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name )';
searchquery = 'searchquery is '+searchquery ;

    ApexPages.Message testMsg = new ApexPages.Message(ApexPages.Severity.WARNING, searchquery);
     ApexPages.addMessage(testMsg);
    

 

 

Second one may be .... like this..

 

List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name)];
      
Account[] accts= ((List<Account>)searchlist[0]);
 

All Answers

ShakilShakil

First query is having small change in escape character, as you missed opening and closing single quotes

 

it shud be as below

 

    String searchText = 'Acme';

 

String wildcardSearchText='%'+ searchText + '%';
        
string searchquery = 'FIND \'' + wildcardSearchText + '\' IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name )';
searchquery = 'searchquery is '+searchquery ;

    ApexPages.Message testMsg = new ApexPages.Message(ApexPages.Severity.WARNING, searchquery);
     ApexPages.addMessage(testMsg);
    

 

 

Second one may be .... like this..

 

List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name)];
      
Account[] accts= ((List<Account>)searchlist[0]);
 

This was selected as the best answer
mavsmavs

Thank you...

I have another question though...

 Can we specify the query to search in custom fields?

for example, I want to search the text in Name and DBA__c fields. I modified the query as below but i keep getting

unexpected token: ,  error

List<List<SObject>> searchList = [FIND :searchText IN Name,DBA__c FIELDS RETURNING Account(Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name)];

 

 

 

Please advise

 

Thanks

WesNolte__cWesNolte__c

Hey

 

There are some nice simple examples here.

 

Wes 

WesNolte__cWesNolte__c
And unfortunately no you can't. Sucks a bit that you can't :(
mavsmavs
Thank you. Hope salesforce implements this in near future