+ Start a Discussion
KMK91KMK91 

With SOSL you can search against different object types that may have similar data, such as contacts and leads.create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter

Best Answer chosen by KMK91
Amit Chaudhary 8Amit Chaudhary 8
Use the Salesforce Object Search Language (SOSL) to search your organization’s Salesforce data for specific information. WithSOSL, you can search all objects—including custom objects—that you have access to in a single query.
SOSL allows you to specify the following for source objects:
-text expression
-scope of fields to search
-list of objects and fields to retrieve
-conditions for selecting rows in the source objects
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_about.htm

Please try below code. I hope that will help you
public class ContactAndLeadSearch{

    public static List<List< SObject>> searchContactsAndLeads(String first)
    {
         List<List<sObject>> searchList=[FIND 'Smith' IN ALL FIELDS RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
            return searchList;
     }
}
Please let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Use the Salesforce Object Search Language (SOSL) to search your organization’s Salesforce data for specific information. WithSOSL, you can search all objects—including custom objects—that you have access to in a single query.
SOSL allows you to specify the following for source objects:
-text expression
-scope of fields to search
-list of objects and fields to retrieve
-conditions for selecting rows in the source objects
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_about.htm

Please try below code. I hope that will help you
public class ContactAndLeadSearch{

    public static List<List< SObject>> searchContactsAndLeads(String first)
    {
         List<List<sObject>> searchList=[FIND 'Smith' IN ALL FIELDS RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
            return searchList;
     }
}
Please let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Mandodari RawatMandodari Rawat
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads (String s) {
            List<List<SObject>> searchlist=[FIND 'Smith' IN ALL FIELDS
                                            RETURNING Contact(FirstName,
                                            LastName WHERE (FirstName =:s
                                                    OR LastName =:s)),       
                                            Lead(FirstName,LastName
                                              WHERE (FirstName =:s
                                                    OR LastName =:s))]; 
            return searchList;
        }
}
Hope this will help.
Bhagat SinghBhagat Singh
Best Answer chosen by Madhukar doesn't use parameter anywher in the code. I think Mandodari Rawat code is correct in my opinion.