3

How do we write SOSL that searches lookup fields. Example My query looks this.

"FIND \'Mark\' in all fields returning Account(Id, Name, User.Name)"

Here it doesn't search User with keyword Mark.

Thanks

2 Answers 2

2

SOSL doesn't search "lookup" fields. For example:

FIND {Acme} IN ALL FIELDS RETURNING Contact(Id, FirstName, LastName, Account.Name)

Won't return contacts with the account called Acme; it will return any contacts that have any reference to Acme in the description field, name fields, etc.

Use a regular SOQL if you want to find accounts owned by a user:

SELECT Id, Name FROM Account WHERE Owner.Name LIKE 'Mark%'

Or, use SOSL with another term, and use an additional WHERE clause:

FIND {term} IN ALL FIELDS RETURNING Account(Id, Name, Owner.Name WHERE Owner.Name LIKE 'Mark%')
2
  • I guess, SOSL for standard object relationship is working(however, on custom object it doesn't work despite of Lookup or MD relationship). Please confirm. Have tested standard object with following query(Burlington is account name): FIND {Burlington} IN ALL FIELDS RETURNING Contact
    – Ysr Shk
    Commented Oct 16, 2018 at 9:34
  • sfdxfox, the problem is that it will only support an AND operation between the FIND and WHERE clauses, which is annoying. The best solution I have found is to do a SOQL query and just combine the lists. It avoids creating triggers to copy data around as SOSL cannot search formula fields. Commented Aug 28, 2020 at 17:07
0

This may have something to do with the OWD on the User Object and your sharing settings on User in your Org (esp if it's set to private). As of SU15, sharing is respected in all classes for the User Object.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .