I have a question regarding a SOSL statement used with a Limit of 1 in trying to search for an email address in either contact or lead records but I only want to return the first occurrence using the LIMIT and regardless of the order of the SFDC objects used within the RETURNING statement. IN the following the query, the lead record email will not be found by the SOSL query:

FIND {"vicki@crownroyale.com"}
                IN EMAIL FIELDS
                RETURNING *Contact*(Id),Lead(Id) LIMIT 1
  1. The lead Id will not be returned unless I swap the contact object with the lead object in the RETURNING statement such that the Lead(Id) is first using LIMIT 1 like this:

FIND {"vicki@crownroyale.com"} IN EMAIL FIELDS RETURNING Lead(Id),Contact(Id) LIMIT 1

  1. Or, it will work using the following SOSL but I risk having multiple results:

FIND {"vicki@crownroyale.com"} IN EMAIL FIELDS RETURNING Contact(Id),Lead(Id)

Question: Is there a way I can run this SOSL statement using a LIMIT 1 without having to worry about the order of the 2 Salesforce objects in the RETURNING statement?


1 Answer 1


You can return one from each if you want:

FIND {emailaddress} 
RETURNING Lead(Id LIMIT 1), Contact(LIMIT 1)

Note that search order is dependent on the SOSL query, so if there's both a Lead and Contact match, then the outer LIMIT 1, as you've discovered, returns the left-most object in the SOSL statement.

  • thanks @sfdcfox. The solution is stable. That is exactly what I needed. Feb 20, 2021 at 5:03
  • Sometimes the SOSL statement grabs the converted lead ID instead of the contact ID. How can I make sure that I am only getting the contact ID? Is that also based on the outer limit ? Thanks Feb 25, 2021 at 2:41
  • @AnthonyWilliams You can add a where clause just you would a query: FIND {emailaddress} IN EMAIL FIELDS RETURNING Lead(Id WHERE IsConverted = FALSE LIMIT 1), Contact(Id LIMIT 1)
    – sfdcfox
    Feb 25, 2021 at 2:55
  • OK @sfdcfox I will test that out. Cool! Feb 25, 2021 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.