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
Unnamalai SubramanianUnnamalai Subramanian 

dynamic SOSL in Apex Class

Hi, 
I am trying to use dynamic search string in Apex class and while executing below code, I don't see the log window. It executes without error but no log window comes up. What am I missing?

Class:
Public Class ContactAndLeadSearch{
    Public static List<List<sObject>> searchContactsAndLeads(String searchword){
        String searchQuery = 'FIND \'' + searchword + '\' IN NAME FIELDS RETURNING Lead(Name where FirstName = \'' + searchword + '\' or LastName = \'' + searchword + '\'),Contact(FirstName,LastName where FirstName = \'' + searchword + '\' or LastName = \'' + searchword + '\')';
        List<List<sObject>> searchConLead = search.query(searchQuery);
        return searchConLead;
           }
}
In Debug Annonymous window:
List<List<sObject>> searchContactLead = ContactAndLeadSearch.searchContactsAndLeads('Smith');

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);
}
 
Best Answer chosen by Unnamalai Subramanian
Amit Chaudhary 8Amit Chaudhary 8
Hi Unnamalai Subramanian,

Please try below class:-
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;
        }
}

NOTE:- if you want to search same keyword in mutliple field then dnt add where you can try IN ALL FIELDS.

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);
}

You can also try workbanch.
https://workbench.developerforce.com/login.php
To Execute Annonymous code like below :-
User-added image

Above code is working fine for me.


Please mark this as Best Answer if this will help you.

Thanks
Amit Chaudhary

All Answers

KaranrajKaranraj
Unnamalai - Are you checking developer console? sometimes log won't get populated in developer cosole, you might want to uncheck and recheck the checkbox "show my current log only" under Debug menu in the developer console else go to Setup->Logs->Debug Logs click 'New' in the monitor user and select your name and save, now run the code in the developer console and check the log file under the Setup->Logs->Debug Logs. 

Try this chrome plugin for checking log file easily - https://chrome.google.com/webstore/detail/salesforce-developer-tool/fiaakhiohminpblhmlihfcdhclmphjcd

Thanks,
Karanraj (http://www.karanrajs.com)
Amit Chaudhary 8Amit Chaudhary 8
Hi Unnamalai Subramanian,

Please try below class:-
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;
        }
}

NOTE:- if you want to search same keyword in mutliple field then dnt add where you can try IN ALL FIELDS.

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);
}

You can also try workbanch.
https://workbench.developerforce.com/login.php
To Execute Annonymous code like below :-
User-added image

Above code is working fine for me.


Please mark this as Best Answer if this will help you.

Thanks
Amit Chaudhary
This was selected as the best answer
Unnamalai SubramanianUnnamalai Subramanian
Thank you Karanraj and Amit!!

Karan: I tried your suggestion of adding my name in Setup>Log>Monitor Log and Debug window opened this time. Also Salesforce Developer Tool suite is awesome!! The debug window showed the right set of records :)

Amit: Thank you and will try in ALL fields if I have to search for multiple fields. Will check out the workbench as well.

Regards,
Unna
Andrew EversleyAndrew Eversley
@ Amit Chaudhary8, thanx, your solution 2 posts up worked for me.