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
MSSMSS 

Should SOSL searches return child Contact records of Accounts that match the search criteria when returning only Contacts?

Should SOSL searches return child Contact records of Accounts that match the search criteria when returning only Contacts?

Let's say you have an Account and Contact records represented by the following code:
Account smithServices;
Contact jane;
Contact mary;
Contact michael;

smithServices = new Account(Name = 'Smith Services');
insert smithServices;

jane = new Contact(FirstName = 'Jane', LastName = 'Ada', Account = smithServices);
mary = new Contact(FirstName = 'Mary', LastName = 'Smith');
michael = new Contact(FirstName = 'Michael', LastName = 'Smith');
insert new List<Contact>{jane, mary, michael};
Then perform the following SOSL search returning only Contact records:
List<List<SObject>> searchList = [FIND 'smith' IN NAME FIELDS RETURNING Contact(Name)];

for (SObject contact : searchList[0]) {
    System.debug(contact);
}
The Contact records returned from the SOSL search are:
Contact:{Name=Michael Smith}
Contact:{Name=Mary Smith}
Contact:{Name=Jane Ada}
I would expect the 'Jane Ada' Contact to not be in the results because that record doesn't include a name field that matches the search criteria.  It looks like it is included in the search results because the parent Account record's Name field matches the search criteria.  Is this expected behavor?


 
Alain CabonAlain Cabon
Hi,

It is not logical but for Contact and Opportunity (at least), there is this wrong behavior.

AccountId searchPrefilterable = true,    The searchPrefilterable field of the fields property now indicates whether a foreign key can be included in prefiltering when used in a SOSL WHERE clause. Prefiltering filters by a specific field value before executing a full search query.

... perhaps this wrong behavior is linked with the property searchPrefilterable = true of AccountId (side effect ?) but Case is not affected.

One workaround (not your question) is to filter the results but that is a problem because of the limit of 2,000 records reduced by this wrong behavior.
List<List<Contact>> searchList = [FIND 'smith' IN NAME FIELDS RETURNING Contact(Name,Account.Name)];
for (Contact contact : searchList[0]) { 
  if (contact.Name.replaceAll('(?i)smith','').length() != contact.Name.length())
    System.debug(contact);
}

I didn't find the rule for this wrong behavior in the documentation of SOSL by using these specific relationships between standard objects and the SOSL requests IN NAME FIELDS.
 
Ajay K DubediAjay K Dubedi
Hi Mss,
The following code working for me. I think you should try this.
 
public class ContactSearchSeccess {
    public static void contact_Search(){
        Account smithServices;
        Contact jane;
        Contact mary;
        Contact michael;
       
        smithServices = new Account(Name = 'Smith Services');
        insert smithServices;
       
        jane = new Contact(FirstName = 'Jane', LastName = 'Ada', Account = smithServices);
        mary = new Contact(FirstName = 'Mary', LastName = 'Smith');
        michael = new Contact(FirstName = 'Michael', LastName = 'Smith');
        insert new List<Contact>{jane, mary, michael};
            List<List<SObject>> searchList = [FIND 'smith' IN NAME FIELDS RETURNING Contact(Name)];
       
        for (SObject contact : searchList[0]) {
            System.debug(contact);
        }
    }
   
}

This is the output of my code.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Jill GuidryJill Guidry
Heya.Thanks for the code.Actaully i was also facing same problem.And this code helped me alot.Thank you https://www.myccpay.net/
fordabo fyifordabo fyi
Thanks for sharing the useful post and you explained in a detailed way. Nice to see this here. To know more about the various apps like skype you can check it here. https://cracko.org/voice-changer-for-skype/
Nageon DeNageon De

I also want to start the coding, how should I begin? Waiting for response

myccpay login (https://cashbytes.org/myccpay-login)

Akshay Verma 33Akshay Verma 33
Thank you so much for these amazing codes. Mythdhr https://thenewviral.com/mythdhr/
carn matcarn mat
PerYourHealth is an online bill payment portal, meant to pay your hospital bills and expenses. It is an efficient and confidential payment mode for your medical bills. Peryourhealth is a popular and trusted method to save oneself from such agony. It is a time-saving and cashless billing platform for a hospital bill that is available 24 hours, provided one has access to a laptop or smartphone. https://peryourhealth.vip