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
Amol Jadhav 34Amol Jadhav 34 

SOSL on ListView not working as expected in Apex

List<List<SObject>> searchList = [FIND 'Barr' IN ALL FIELDS RETURNING Contact(Id, Name USING ListView=TestNamespace__Test_View ORDER BY Name ASC)]

The above SOSL works fine, but find 'Barr' in all contacts not only in 'Test_View', as documentation it should search only in 'Test_View'.
 
Best Answer chosen by Amol Jadhav 34
Amol Jadhav 34Amol Jadhav 34

Hi Devi,

I have found the solution:

List<List<SObject>> searchList = [FIND {Barr} IN ALL FIELDS RETURNING Contact(Id, Name USING ListView=TestNamespace__Test_View ORDER BY Name ASC)] 
Using above SOSL 'Barr' will only be searched in 'TestNamespace__Test_View' not in all contacts

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Amol,
I tried this in my org.Looks like this is an issue in salesforce.
When executing in query editor i am getting correct results.When i am executing from anonymous window getting records which are not in the list view also.

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Amol Jadhav 34Amol Jadhav 34

Hi Devi,

I have found the solution:

List<List<SObject>> searchList = [FIND {Barr} IN ALL FIELDS RETURNING Contact(Id, Name USING ListView=TestNamespace__Test_View ORDER BY Name ASC)] 
Using above SOSL 'Barr' will only be searched in 'TestNamespace__Test_View' not in all contacts
This was selected as the best answer