1

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 {"[email protected]"}
                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 {"[email protected]"} 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 {"[email protected]"} 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?

Thanks

1 Answer 1

1

You can return one from each if you want:

FIND {emailaddress} 
IN EMAIL FIELDS 
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.

4
  • thanks @sfdcfox. The solution is stable. That is exactly what I needed. Commented 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 Commented 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
    Commented Feb 25, 2021 at 2:55
  • OK @sfdcfox I will test that out. Cool! Commented Feb 25, 2021 at 19:18

You must log in to answer this question.

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