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

Is it possible to use apex variables in SOSL?

After two years of writing Apex I thought I finally found a use for sosl, but it seams that it's not possible to search using a variable as I have become used to in SOQL. Can anybody tell me if the following is possible, or do I have to put the sosl query inside of a for loop so that I can get the string which makes up the keySet().



List<List<sObject>> leadsYContacts = [FIND :newLeadsMap.keySet() IN Email Fields Returning Lead,Contact];


The variable newLeadsMap is a Map<String,Lead>. The string being the Lead's email. I am trying to make a trigger that finds existing contacts and leads that have the same email as incoming leads.



Firstly, I think SOSL is the wrong tool for the job here, there's no guanratees as to when the SOSL search database is updated, so if somebody creates contactA with an email of, and 25 seconds later somebody creates contactB with the same email there's no guarantee, and it's actually unlikley that SOSL would turn up contactA. SOSL is also a pain to test for this reason as well.


I'd say it's better to just do two SOQL queries (to contact and lead) in this case as you know both the string to look for and the field(s) to check. SOSL is really purpose built for things like searching across ALL object types, or driving custom search from visualforce.


But I believe your SOSL synatax is also off, here's a snippet from my code:


List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING 
    		Contact(id, name)];
    	Contact = ((List<Contact>)searchList[0]);

Try specifying the fields to return on contacts and leads and see if that helps if you continue to use SOSL.



The information in your first paragraph was helpful news for me. I didn't realize that sosl was not querying live data, but instead queries a regularly updated database.


I probably will go with the two soql queries. However, what I really want to know still remains unclear.

You suggested that the error in my syntax was due to the fact that i was not specifying which fields to include. I actually am doing this in my code but for the post I decided to take out the fields to reduce clutter and the documentation seemed to indicate that it was proper syntax.


Anyway, I am still curious to know if I can use an Apex variable like I have done with FIND map.keySet().

It's not clear to me from your snippet of FIND : searchText.  And, all of the examples that I can find in documetation use a literal string.



Jitender  PaddaJitender Padda
Maybe the Following example will help clear up things for you -  
public with sharing class ContactAndLeadSearch {
    public static List<List< SObject>> searchContactsAndLeads(String search){
        List<List< SObject>> conLeadList= new List<List< SObject>>();
        conLeadList=[Find :search IN ALL FIELDS RETURNING lead,contact];
        return conLeadList;