You need to sign in to do that
Don't have an account?
Rishabh Agrawal
Trailhead challenge : create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(string FN)
{
List<List<sObject>> searchList = [FIND 'FN' IN all fields
RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
//Contact[] searchContacts = (Contact[])searchList[0];
//Lead[] searchLeads = (Lead[])searchList[1];
return searchList;
}
}
Please someone correct this code ,, this one was nt fulfilling the challenge req.
public static List<List<SObject>> searchContactsAndLeads(string FN)
{
List<List<sObject>> searchList = [FIND 'FN' IN all fields
RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
//Contact[] searchContacts = (Contact[])searchList[0];
//Lead[] searchLeads = (Lead[])searchList[1];
return searchList;
}
}
Please someone correct this code ,, this one was nt fulfilling the challenge req.
Hope you are trying the below trailhead contest,
https://developer.salesforce.com/trailhead/force_com_dev_beginner/apex_database/apex_database_sosl?state=id
Make sure the following inorder to complete your challenge,
1. You must create a Contact record and Lead record before checking this challenge. Both records must have the last name 'Smith'.
Below code is working fine.
Before go to the challenge create contact and lead with last name as "Smith"
public class ContactAndLeadSearch
{
public static List<List<SObject>> searchContactsAndLeads(String str)
{
List<List<sObject>> searchContactsAndLeadsList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
return searchContactsAndLeadsList ;
}
}
Thanks & Regards,
Satya P
Arunkumar R
In This Below code is Not working please to be slove in This error
The error to be show in
"Challenge Not yet complete... here's what's wrong:
The Lead and Contact records with the last name 'Smith' were not found. Please add these records for this challenge."
public class ContactAndLeadSearch
{
public static List<List<SObject>>searchContactsAndLeads(String str)
{
List<List<sObject>> searchList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
return searchList;
}
}
To solve this challenge firstly u have to create an record in Contact and Lead.But when creating records we can give first name any thing but last name should be Smith in both records. This Answer will solve the challenge
Thanks
Nirmallya Ghosh
public class ContactAndLeadSearch
{
public static List<List<SObject>> searchContactsAndLeads(String str)
{
List<List<sObject>> searchContactsAndLeadsList = [FIND :str IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
return searchContactsAndLeadsList ;
}
}
After writing this code, create the last name as smith in lead and contact, then this challenge will get success.
You can use the code below:
This is working like magic for me. Try it.
Hope this helps!!
Then create the class as follows:
Public class ContactAndLeadSearch{
Public static list<list<sobject>> searchContactsAndLeads(String strName){
List<List<sobject>> searchList =[FIND :strName IN ALL FIELDS RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];
return searchList;
}
}
New to this but very frustrated, I have created Contacts and Leads and pretty sure the code is right, it just keeps failing saying that the Contacts and Leads are not there!
public class ContactAndLeadSearch
{
public static List<List<SObject>> searchContactsAndLeads(String SearchValue)
{
List<List<SObject>> ContactsandLeadsList =
[FIND :SearchValue IN ALL FIELDS
RETURNING
Contact(FirstName,LastName),
Lead(Name,LastName,Company)];
return ContactsandLeadsList;
}
}
The Lead and Contact records with the last name 'Smith' were not found. Please add these records for this challenge. Had similar problems with other challenges, any thoughts?
Lead lead = new Lead(
FirstName = 'John',
LastName = 'Smith'
Company = 'Company Name'
);
First create a contact and a lead record with last name as smith .
public class ContactAndLeadSearch {
public static list<list<sobject>> searchContactsAndLeads(string lname)
{
list<list<sobject>> LS = [FIND :lname in Name Fields returning Lead(lastname,firstname where
lastname =: lname or firstname =: lname
),
contact(lastname,firstname where
lastname =: lname or firstname =: lname)];
system.debug('Lead-->'+LS[0]);
system.debug('Contact-->'+LS[1]);
return LS;
}
}
IN Open Execute Anonymous Window :
ContactAndLeadSearch.searchContactsAndLeads('smith');
Try the below code, this works for me to complete the Trailhead challenge
public class ContactAndLeadSearch{
public static List<List< SObject>> searchContactsAndLeads(String Smith){
List<List<SObject>> searchList = [FIND 'Smith' IN ALL FIELDS
RETURNING Lead(LastName), Contact(LastName) ];
return searchList ;
}
}
Note: Create one Lead record and one Contact record in your Org having LastName = 'Smith' and then check the challenge.
HI all
i am facing an error while inserting contact record.
could you please help me
lead ad=new lead(firstname='jan',LastName='smith');
insert ad;
Arunkumar R,
Thanks!.
While executing few classes in anonymous block, getting this error. {static can only be used on methods of a top level type}
Can you please suggest why this occurs what we should to avoid this?
Pay close attention to LEAD...to execute the function a new lead has a "required" Company record. If you use the code I provided but you attempt to remove ,Company='YourCompany' it will give you and error upon attempting step #4. Try it and see...That's how I learned Lead requires Company to upsert.
You must complete each step. If you do not debug->>execute the function it will not update the record and you'd have to do it manually as others suggest in the post above.
1st step: Insert this code
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(String name){
Contact con = new Contact(LastName='Smith');
upsert con;
Lead le = new Lead(LastName='Smith',Company='YourCompany');
upsert le;
List<List<SObject>> searchList = [Find :name IN NAME FIELDS RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];
return searchList;
}
}
2nd step: Save
3rd step: Debug->Open Execute Anonynmous Window: insert this code
ContactAndLeadSearch.searchContactsAndLeads('Smith');
4th step: Press Execute
5th step: You're done -> Check challenge to earn 500 points
Hi
Can someone please explain the below code line 2 and 3?
List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS RETURNING Account(Name),Contact(FirstName,LastName,Department)];
Account[] searchAccounts = (Account[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
public static List<List<sObject>> searchContactsAndLeads(String incomingParam){
List<List<sObject>> searchList = [FIND : incomingParam IN ALL FIELDS
RETURNING Contact(FirstName, LastName),Lead(FirstName, LastName)];
return searchList;
}
}
above code is working.
i see everyone has used "IN ALL FIELDS" as the SEARCH GROUP.
If I am reading it correct, the ask is to search in FIRST and LAST NAME only. Hence, we can try "IN NAME FILEDS" as the SEARCH GROUP.
I did that and it was accepted..
public class ContactAndLeadSearch{
public static List<List< SObject>> searchContactsAndLeads(String Smith){
List<List<SObject>> searchList = [FIND 'Smith' IN ALL FIELDS
RETURNING Lead(LastName), Contact(LastName) ];
return searchList ;
}
}
Thanks @ AnantGarg, For giving simple way to creating records
Lead led = new Lead(
FirstName='Any',
LastName='Smith',
Company='myCompanyl');
insert led;
Contact cnt = new Contact(
FirstName='Any',
lastName='Smith');
insert cnt;
public class ContactAndLeadSearch
{
public static List<List< SObject>> searchContactsAndLeads(string var1)
{
List<List< SObject>> conts = [FIND :var1 IN ALL FIELDS
RETURNING Contact(FirstName,LastName WHERE FirstName = :Var1 OR LastName = :Var1),Lead(FirstName,LastName WHERE FirstName = :Var1 OR LastName = :Var1)];
System.debug(conts);
return conts;
}
}
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(String str)
{
Contact con = new Contact(LastName='Smith');
upsert con;
Lead le = new Lead(LastName='Smith',Company='YourCompany');
upsert le;
List<List<SObject>> searchList = [FIND 'str' IN ALL FIELDS
RETURNING Contact(FirstName,LastName), Lead(FirstName,LastName)];
return searchList;
}
}
Thanks, Aayushi.
public class ContactAndLeadSearch {
public static List<List< SObject>> searchContactsAndLeads(string searchText){
List<List<sObject>> searchList = [FIND :searchText IN ALL FIELDS
RETURNING
Contact(FirstName,LastName),
Lead(FirstName,LastName)];
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;
}
}
Please mark helpful once done.
Thanks & Regards,
Rahul Gupta.
This Worked for me I hope it will work for you guys as well.
(Before doing the below steps make sure you have created "Smith" as the 'Last Name' in 'Contact' as well as 'Lead')
Follow the steps:
Step 1: Go into the Developer Console > Apex class >ContactAndLeadSearch
Step 2: Follow the below syntax.
public class ContactAndLeadSearch {
public static list<list<sobject>> searchContactsAndLeads(string lname)
{
list<list<sobject>> LS = [FIND :lname in Name Fields returning Lead(lastname,firstname where
lastname =: lname or firstname =: lname
),
contact(lastname,firstname where
lastname =: lname or firstname =: lname)];
return LS;
}
}
Step 3: For debug execution.
ContactAndLeadSearch.searchContactsAndLeads('Smith');
Thank you!!!
Sameer Macwan
============================================================================================================
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(String FLname)
{
List<List<sObject>> searchList = [FIND :FLname IN ALL FIELDS RETURNING Contact(FirstName,LastName) ,Lead(FirstName,Lastname)];
System.debug(searchList.size() + ' contacts & Leads are returned.');
return searchList;
}
}
=============================================================================================================
Please mark helpful once done.
Thanks & Regards
Amol K
//Try this
public class ContactAndLeadSearch {
public static List<List<sObject>> searchContactsAndLeads(String str){
list<list<sObject>> lis=[FIND : str IN ALL FIELDS RETURNING Contact(FirstName,LastName),Lead(FirstName,LastName) ];
return lis;
}
}
try this
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(String searchText)
{
List<List<sObject>> searchList = [FIND :searchText IN ALL FIELDS
RETURNING
Contact(FirstName,LastName),
Lead(FirstName,Lastname)];
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;
}
}
Anonymous Window
ContactAndLeadSearch.searchContactsAndLeads('smith');
Please mark it as a best answer!
Thanks!
public class ContactAndLeadSearch {
public static List<List< sObject>> searchContactsAndLeads(String name){
return [FIND :name IN ALL FIELDS RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
}
}
create new class
public class ContactAndLeadSearch
{
public static List<List< sObject>> searchContactsAndLeads(string srchString)
{
List<List<sObject>> searchList = [FIND :srchString IN ALL FIELDS
RETURNING Lead(FirstName,LastName),Contact(FirstName,LastName)];
Lead[] searchLeads = (Lead[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('Found the following accounts.');
for (Lead l : searchLeads)
{
System.debug(l.LastName + ', ' + l.FirstName);
}
System.debug('Found the following contacts.');
for (Contact c : searchContacts)
{
System.debug(c.LastName + ', ' + c.FirstName);
}
return searchList;
}
}
Execute:
ContactAndLeadSearch.searchContactsAndLeads('smith');