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
MenteeMentee 

SOSL query - Lookup fields

String query = 'FIND \'*' + searchString + '*\' IN ALL FIELDS RETURNING Ticket__C (Region__r.name, Manager__c, Manager__r.name, Name, Notes__c), user(id,Name)'; 
 I am searching with user name and results are zero.
 Aim: when a string is entered, it should match with any of the fields of ticket and display the results. with the above query, it will match only with Ticket.name and shows zero results when i am searching for the user.name.  any inputs please
Best Answer chosen by Mentee
MenteeMentee
That is what I did. String query = 'FIND \'*' + searchString + '*\' IN ALL FIELDS RETURNING Ticket__C (Region__r.name, Manager__c, Manager__r.name, Name, Notes__c), user(id,Name)'; 

Manager__c  is a Lookup(User).
This how I should do?
Ticket__c[] searchTickets= (Tickets__c[])searchList[0];
User[] searchUser = (User[])searchList[1];
in for loop user(){
 if(user.name == manager name in ticket){
 addTolistToDisplay.add(ticket__c object);
}

or any other efficient way I can do this?

All Answers

Raj VakatiRaj Vakati
In SOSL you can't search by string value of a Lookup field.



In SOSL you can't search by string value of a Lookup field.

For Example:

Record in object Contact:
Name: David Sidhu

Now there's a related object "LookUpToContact" looking up to parent object Contact.

Try to search "David Sidhu" using SOSL in LookUpToContact Query:

String searchName = 'FIND {David*} IN ALL FIELDS RETURNING LookUptoContact__c(Id, Contact__r.FirstName)';

Returns nothing, just an error (can be verified using workbench).


https://help.salesforce.com/articleView?id=000176100&type=1
MenteeMentee
 Thanks @Raj Vakati for your reply. I understand it can not be done. but how to acheive the goal.
I have a search which has to find given string from the Ticket object which also has lookup field to user. 
If input is a user name, they should be able to see ticket records related to the user, which I am displaying on visualforce.
Raj VakatiRaj Vakati
Try like this 

 
String query = 'FIND \'*' + searchString + '*\' IN ALL FIELDS RETURNING Ticket__C (Name, Manager__c,Name, Notes__c),
Region__c(Id,Name),Manager__c(Id,Name), user(id,Name)';

 
MenteeMentee
That is what I did. String query = 'FIND \'*' + searchString + '*\' IN ALL FIELDS RETURNING Ticket__C (Region__r.name, Manager__c, Manager__r.name, Name, Notes__c), user(id,Name)'; 

Manager__c  is a Lookup(User).
This how I should do?
Ticket__c[] searchTickets= (Tickets__c[])searchList[0];
User[] searchUser = (User[])searchList[1];
in for loop user(){
 if(user.name == manager name in ticket){
 addTolistToDisplay.add(ticket__c object);
}

or any other efficient way I can do this?
This was selected as the best answer
Raj VakatiRaj Vakati
I dnt think so there is any other way ...