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

Writing Tests for SOSL don't forget the Test.setFixedSearchResults method

Just spent the past few hours trying to to get a method this uses SOSL to pass, but it kept returning zero rows.


So I check the code by running it in the Execute Anonymous window in the IDE and it worked!


I check the permissions - not an issue. I was stumped, then I stumbled upon this little help article hidden away at the back of the manual on Test.setFixedSearchResults

See Salesforce URLS


Ok so heres how it works in a test method:


//create your sobject with some test data

myobject o = new myObject(name='test');


//setup some ID's for the SOSL

 id[] fixedSearchResults = new id[1];
   fixedSearchResults[0] =;


//Call the method on the controller that calls the SOSL query



//do your asserts




Hi ,


    You are right to ensure that test methods always behave in a predictable way, any 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.


All SOSL queries that take place later in the test method return the list of record IDs that were specified by the


method. Additionally, the test method can call


multiple times to define different result sets for different SOSL queries. If you do not call the


method in a test method, or if you call this method without specifying a list of record IDs, any SOSL queries that take place later in the test method return an empty list of results.


The list of record IDs specified by the Test.setFixedSearchResults method replaces the results that would normally be returned by the SOSL query if it were not subject to any WHERE or LIMIT clauses. If these clauses exist in the SOSL query, they are applied to the list of fixed search results. For example:

<script type="text/javascript">// swfobject.registerObject("clippy.d13109e79", "9"); // </script>
 public class SoslFixedResultsTest1 {

public static testMethod void testSoslFixedResults() {Id [] fixedSearchResults= new Id[1];
fixedSearchResults[0] = '001x0000003G89h';
List<List<SObject>> searchList = [FIND 'test'
Account(id, name WHERE name = 'test' LIMIT 1)];