Returning Contacts and Leads with Custom Wrapper Class

Unfortunately alot of companies use Leads and Contacts interchangeably. Here is a small Apex class that performs a SOSL search across Contacts and Leads by email address. The method then wraps each Contact and Lead as a generic Person object and returns them to the caller as a List.

public class PersonService {

 public class Person {

  String id;
  String firstName;
  String lastName;
  String company;
  String email;
  String phone;
  String sObjectType;


 public static List searchByEmail(String email) {

  // list of Person objects to return
  List people = new List();

  // issue the sosl search
  List<list> searchResults = [FIND :email IN EMAIL FIELDS RETURNING
   Contact (Id, Account.Name, Email, Phone, FirstName, LastName),
   Lead (Id, Company, FirstName, LastName, Email, Phone)];

  // cast the results by sObjec type
  List contacts = ((List)searchResults[0]);
  List leads = ((List)searchResults[1]);

  // a each contact found as a Person
  for (Integer i=0;i<contacts.size();i++) {
   Person p = new Person(); = contacts[i].Id;
   p.firstName = contacts[i].FirstName;
   p.lastName = contacts[i].LastName; = contacts[i].Account.Name; = contacts[i].Email; = contacts[i].Phone;
   p.sObjectType = 'Contact';

  // a each lead found as a Person
  for (Integer i=0;i<leads.size();i++) {
   Person p = new Person(); = leads[i].Id;
   p.firstName = leads[i].FirstName;
   p.lastName = leads[i].LastName; = leads[i].Company; = leads[i].Email; = leads[i].Phone;
   p.sObjectType = 'Lead';

  System.debug('Returning people: '+people);

  return people;