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
TehNrdTehNrd 

SOSL query not returning values when used in testMethods

Here is the code but it appears the SOSL query is not returning the account that was just inserted:

Code:
public class soslTest {
 
 public static testMethod void SOSLtest(){
  
  Account acct = new Account(
   Name = 'Test Account'
  );
  insert acct;
 
  List<List<SObject>> searchList = [FIND '*Test Account*' IN NAME FIELDS RETURNING Account(Id, name)]; 
  Account [] accounts = ((List<Account>)searchList[0]);
  system.debug('Accounts ' + accounts);
 }
}

//And the debug line:

20080610183243.419:Class.soslTest.SOSLtest: line 12, column 9: Accounts ()

 Any ideas?

Rusty12Rusty12
SOSL is a special case.  Check the latest Apex Language Reference PDF for more info on SOSL in testMethods.  There is a section (currently on page 124) in the PDF called "Adding SOSL Queries to Unit Tests".

Code:
To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that
is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the
query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of
record IDs that are returned by the search...

 

TehNrdTehNrd
Thanks for pointing that out. When this document first came out it was around 100 pages but as it starts to push beyond 300 pages it's getting harder to take it all in.

-Jason