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
adrisseladrissel 

SOSL Query for Contacts by Account Name

Hey all.  I am trying to refine a search functionality I have developed.  I want the search string the user enters to search ALL FIELDS to return as many relevant results as possible.  However, I am expecting certain data to show that aren't.  For example, I have an account with the Account Name of "Worldwide Acme Holdings".  The user is entering the search string "acme".  There are lots of Contacts associated with that account.  However, the search results do not yield any of those contacts.  Here is my query:
 
allAccsCons = [FIND :searchString IN FIELDS RETURNING Contact(Name,Id,AccountId,Email,Phone,Account.Name),Account(Id,Name,Type,BillingCity,BillingState,Industry)];
It doesn't seem that the SOSL query is looking at the "Account.Name" value for the Contact sObject.  The account results themselves do contain the Account mentioned above.  But, I am wanting a contact list as well of all contacts who are part of that account. 

Am I doing something wrong here?  Technically the field with the Account Name label in SF is an Id/Lookup field.  Is that why?  Any viable workarounds?

Thanks!
 
Best Answer chosen by adrissel
BalajiRanganathanBalajiRanganathan
Basically you are expecting the related contact records to be returned while searching. SOSL does not work in that way.It only returns the record that are matched by the search.
Note that RETURNING clause is used to specify the the fields that will be returned from the records after the search operation. those fileds are not used for search. 

In your controller you have to issue a SOQL to get the related contact or account record using the list of id returned by the SOSL.

For valid search group settings look at the link below
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_sosl_in.htm

 

All Answers

BalajiRanganathanBalajiRanganathan
Basically you are expecting the related contact records to be returned while searching. SOSL does not work in that way.It only returns the record that are matched by the search.
Note that RETURNING clause is used to specify the the fields that will be returned from the records after the search operation. those fileds are not used for search. 

In your controller you have to issue a SOQL to get the related contact or account record using the list of id returned by the SOSL.

For valid search group settings look at the link below
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_sosl_in.htm

 
This was selected as the best answer
adrisseladrissel
Thanks Balaji, that's what I suspected.  Good to have this on the blogs anyways for others to see and confirm as well.