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
saran kumar 9saran kumar 9 

Writing SOSL Queries ERROR

this is the error i am getting ........please help me



User-added image
Adrian  GawryszewskiAdrian Gawryszewski
I guess it would be helpful if you actually show us the query.
Anuj PatelAnuj Patel
I will give you the code I have used, You can compare and figure it out. 
Public Class ContactAndLeadSearch
{
        Public static List<List<sObject>> searchContactsAndLeads(String searchword)
        {
            String searchQuery = 'FIND \'' + searchword + '\' IN ALL FIELDS RETURNING Lead(Name,FirstName,LastName ), Contact(FirstName,LastName )';
            List<List<sObject>> searchConLead = search.query(searchQuery);
            return searchConLead;
        }
}


Execute below code in In Debug Annonymous window
 
List<List<sObject>> searchContactLead = ContactAndLeadSearch.searchContactsAndLeads('amit');

List<Lead> leadList = New List<Lead>();
List<Contact> contList = New List<Contact>();

leadList = ((List<Lead>)searchContactLead[0]);
contList = ((List<Contact>)searchContactLead[1]);

for(Lead a:leadList)
{
System.debug('Found following Leads ' + a.Name);
}
for(Contact cts:contList){
System.debug('Found following Contacts ' + cts.FirstName + '' + cts.LastName);
}

Refer to the following link for details description.



Please mark it as the best answer if you find it useful.

Thanks,
Anuj.
 
saran kumar 9saran kumar 9
thanks a lot anuj ....i rectified .....thanks a lot
Anuj PatelAnuj Patel
@Saran

To Help us in return, Please select it as a best answer and like it after that.

Thanks,
Anuj.
Ben RowleyBen Rowley
A Slightly alternative implementation: 

public class ContactAndLeadSearch {

    public static List<List<sObject>> searchContactsAndLeads(String searchTerm){
        List<List<sObject>> contactsAndLeads = [FIND :searchTerm IN ALL FIELDS 
                   RETURNING Contact(FirstName,LastName),Lead(FirstName,LastName)];
        
       System.debug(contactsAndLeads);
        return contactsAndLeads;
    }
}

// Remember then to access static methods by class name, not object name

e.g. from execute anomonous:

ContactAndLeadSearch.searchContactsAndLeads('Smith');