Skip to main content
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);

}

 
4 respostas
  1. 28 de jun. de 2015, 06:14
    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 :-

    Hi Unnamalai Subramanian,Please try below class:-Public Class ContactAndLeadSearch{ Public static List<List<sObject>> searchContactsAndLeads(String searchword) { String searchQuery = 'FIND \'' + searc

    Above code is working fine for me.

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

    Thanks

    Amit Chaudhary