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
Trevor DintinoTrevor Dintino 

Create an Apex class that returns both contacts and leads based on a parameter.

On this one, I have this as my code which I'm pretty sure is correct. But I'm lost because its stating that there are no contacts or leads with that last name. How exactly do I add these?

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


Thanks,

Trevor
Amit Chaudhary 8Amit Chaudhary 8
Hi Trevor Dintino,

First Create one lead and one contact record with last Name Smith .

Your code should be like below
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);
}
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BTk4IAG

Let us know if that will help you

 
Trevor DintinoTrevor Dintino
Amit, I understand the coding part, but I've never made a lead or contact. Wondering if that had to be in code or not. Thanks
Amit Chaudhary 8Amit Chaudhary 8
You need to create lead and contact record in your salesforce org first. Let us know if that will help you
 
sathishkumar periyasamysathishkumar periyasamy
Trevor, You code should work fine. Can you please check security - whether you have access to that record or not. And also try with 'Smith*'.
Srujana ReddySrujana Reddy
Try this,it will helpfull.
First we need to create Contact and Lead record with The LastName Smith And then write below code.
public class ContactAndLeadSearch {
    public static List <List<SObject>> searchContactsAndLeads(String Smith){
    List <List<SObject>> searchlist=[FIND 'Smith' IN ALL FIELDS RETURNING Contact(LastName),Lead(LastName)];
        return searchlist;
  }
}
Maria MachadoMaria Machado
If I wanted to test this code in the anonymous window. What code should I write?
Amit Chaudhary 8Amit Chaudhary 8
try below code
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);
}

 
Teja M 27Teja M 27
public class ContactAndLeadSearch {
    public static List<List< SObject>> searchContactsAndLeads(string name){
        List<List<sobject>> searchList = [FIND :name IN NAME FIELDS 
                                        RETURNING Contact(LastName,FirstName),Lead(LastName,FirstName)];
       	Contact[] searchContacts = (Contact[])searchList[0];
		Lead[] searchLeads = (Lead[])searchList[1];
        System.debug('Found the following Contacts.');
		for (Contact c : searchContacts) {
    	System.debug(c.LastName + ', ' + c.FirstName);
		}
	System.debug('Found the following Leads.');
	for (Lead l : searchLeads) {
    System.debug(l.LastName + ', ' + l.FirstName);
		}
        return searchList;
    }
}

 
Abhirup MukherjeeAbhirup Mukherjee
public class ContactAndLeadSearch {
    public static List<list<Sobject>> searchContactsAndLeads(string smith){
     List<list<Sobject>> c=new List<list<Sobject>>();
        c=[FIND 'smith' IN ALL FIELDS RETURNING Lead(FirstName,Lastname), Contact(FirstName,LastName)];
        return c;
    }
}