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?
Hi Mss,The following code working for me. I think you should try this.
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 Dubedipublic 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);
}
}
}