https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_database/apex_database_sosl#challenge and this is the error message that appears in red:
Challenge not yet complete... here's what's wrong: Executing the 'searchContactsAndLeads' method failed. Either the method does not exist, is not static, or does not return the expected search results.
Here is my class:
//The apex class in public domain public class ContactAndLeadSearch {
//public static method that takes a String parameter
public static SObject[] searchContactsAndLeads(String input) {
//Create list of two lists, one for Contacts and the second for Leads List<List<SObject>> searchResults = [FIND :input IN NAME FIELDS RETURNING Contact(Name), Lead(Name)];
//Place list of contacts at position 1 and leads at 2 Contact[] searchContacts = (Contact[])searchResults[0]; Lead[] searchLeads = (Lead[])searchResults[1];
//return the two lists - I think this is where the problem is. return searchContacts; return searchLeads;
}
}
After fiddling around a little I have managed to get to the stage where no problems appear in the Problems tab in developer console. But I suspect the code would only execute up to first return method on second last line 'return searchContacts;'
Hello Mubashir, I'm Still trying to complete the challenge so I still do not have the final answer, nevertheless I noticed that the challenge indicates: The return type for 'searchContactsAndLeads' must be 'List<List< SObject>>'
You have two returns in a single method, which will not work - you should return a single collection of lists.
1. change the method declaration to have something like this - a collection of collections. public static List<List< SObject>> searchContactsAndLeads(String input){
// I've included in Bold and Italic the return type - simply use this instead of an array like you had in your example.
2. Change the return - You will only have to return the "searchResults" variable you have in your code. Something like return searchResults;
notes - a. You declare a collection of collections in a similar way to a normal collection - the trailhead Apex Basics & Database module on section Writing SOSL Queries, has an example for your reference (Search for "SOSL Apex Example"), and you can find more examples in Salesforce documentation. b. I found a cool tutorial on Java, not Apex (but I'm guessing the behavior similar) on the "return" keyword here: http://www.java-samples.com/showtutorial.php?tutorialid=280 c. From what I understood you don't have to get the individual collections to get the challenge successfully evaluated.
The Problem is with return type when you have List<List<sObject>> you need to define function return type the same.
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;
}
}
Hello again - no worries: I sense that you are mixing "lists" and "arrays". In your code line 6 you have an array declared as indicated by the usage of [], but you are returning a List as indicated by the <> (line 14). The list is initialized in line 10.
You can read a bit more here: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zveIAA
Hello, You have to create new lead reccord with last name = Smith and also convert it without opp to have a contact !
public with sharing class ContactAndLeadSearch { public static List<List<SObject>> searchContactsAndLeads(string param){ List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)]; return (searchConLead); } }
public class ContactAndLeadSearch { public static List<List< SObject>> searchContactsAndLeads(string smith){ List<List<sObject>> searchList = [FIND 'Smith' IN ALL FIELDS RETURNING Lead(LastName WHERE LastName = :'smith'),Contact(LastName Where LastName = :'smith')]; return (searchList);
public class ContactAndLeadSearch { public static List<List<SObject>> searchContactsAndLeads(string param) { List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)]; return (searchConLead); } } /* before calling this method, execute below through anonymous window first
Account acct = new Account ( Name = 'Smith Computing', Phone = '(415 555-1212',
NumberOfEmployees = 50,
BillingCity = 'San Francisco'); insert acct; ID acctID = acct.ID;
Contact con = new Contact ( FirstName = 'Carol', LastName = 'Smith', Phone = '(415) 555-1212', Department = 'Wingo', AccountId = acctID); insert con;
Lead theLead = new Lead( FirstName = 'Jim', LastName = 'Smith', Phone = '(111) 111-1111', Company = 'IGATE',
public class ContactAndLeadSearch { public static List<List<SObject>> searchContactsAndLeads(string userVariable){ List<List<SObject>> result= [FIND :userVariable IN ALL FIELDS Returning Contact(FirstName,LastName), Lead(FirstName,LastName)]; return result; } }
Super. I love useful discussions in which you can find answers to exciting questions. This is very valuable, especially when you need to solve a problem quickly and do not know where to turn. In one of these discussions, I found a site recommendation https://studentshare.org/capstone-project with examples of the Capstone Project, I was so happy, it was a timely find that solved some of my problems at the university, I am sharing it with you, maybe someone will also be relevant.
Here's the link of the challenge page:
https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_database/apex_database_sosl#challenge
and this is the error message that appears in red:
Challenge not yet complete... here's what's wrong:
Executing the 'searchContactsAndLeads' method failed. Either the method does not exist, is not static, or does not return the expected search results.
Here is my class:
//The apex class in public domain
public class ContactAndLeadSearch {
//public static method that takes a String parameter
public static SObject[] searchContactsAndLeads(String input) {
//Create list of two lists, one for Contacts and the second for Leads
List<List<SObject>> searchResults = [FIND :input
IN NAME FIELDS
RETURNING Contact(Name), Lead(Name)];
//Place list of contacts at position 1 and leads at 2
Contact[] searchContacts = (Contact[])searchResults[0];
Lead[] searchLeads = (Lead[])searchResults[1];
//return the two lists - I think this is where the problem is.
return searchContacts;
return searchLeads;
}
}
After fiddling around a little I have managed to get to the stage where no problems appear in the Problems tab in developer console. But I suspect the code would only execute up to first return method on second last line 'return searchContacts;'
Thanks a lot
The return type for 'searchContactsAndLeads' must be 'List<List< SObject>>'
You have two returns in a single method, which will not work - you should return a single collection of lists.
Cheers,
How do i return a collection though?
cheers
1. change the method declaration to have something like this - a collection of collections.
public static List<List< SObject>> searchContactsAndLeads(String input){
// I've included in Bold and Italic the return type - simply use this instead of an array like you had in your example.
2. Change the return - You will only have to return the "searchResults" variable you have in your code.
Something like
return searchResults;
notes -
a. You declare a collection of collections in a similar way to a normal collection - the trailhead Apex Basics & Database module on section Writing SOSL Queries, has an example for your reference (Search for "SOSL Apex Example"), and you can find more examples in Salesforce documentation.
b. I found a cool tutorial on Java, not Apex (but I'm guessing the behavior similar) on the "return" keyword here:
http://www.java-samples.com/showtutorial.php?tutorialid=280
c. From what I understood you don't have to get the individual collections to get the challenge successfully evaluated.
Hope you don't mind carrying on the conversation.
Here's what I get for 'searchResults' - "Return value must be of type List", but to me it is a List.
cheers
The Problem is with return type when you have List<List<sObject>> you need to define function return type the same.
Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
You can read a bit more here: https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zveIAA
Hope this helps!
But there's another trouble now: who do I give 'Best Answer' as only one can be done - oops! a tie!
You need to follow this url to create contact record
http://na3.salesforce.com/003/e and
https://na3.salesforce.com/00Q/e
(replace na3 with your salesforce instance)
Thanks,
Himanshu
You have to create new lead reccord with last name = Smith and also convert it without opp to have a contact !
public with sharing class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(string param){
List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)];
return (searchConLead);
}
}
Hope it help :)
Add the below in the Apex class:
public class ContactAndLeadSearch {
public static List<List< SObject>>searchContactsAndLeads( String str){
List<List<SObject>> searchList = [FIND :str RETURNING Contact(FirstName, LastName), Lead(FirstName , LastName)];
return searchList;
}
}
public class ContactAndLeadSearch {
public static List<List< SObject>> searchContactsAndLeads(string smith){
List<List<sObject>> searchList = [FIND 'Smith' IN ALL FIELDS
RETURNING Lead(LastName WHERE LastName = :'smith'),Contact(LastName Where LastName = :'smith')];
return (searchList);
}
}
public class ContactAndLeadSearch {
public static List<List<sObject>> searchContactsAndLeads(String Srchstring)
{
List<List<sObject>> SrcList=[Find :Srchstring IN all FIELDS returning Contact(Firstname,LastName), Lead(Firstname,LastName)];
return SrcList;
}
}
public class ContactAndLeadSearch {
public static List<List<Object>> searchContactsAndLeads (String str)
{
List<List<Object>> obj = [FIND :str IN ALL FIELDS
RETURNING Contact(FirstName,LastName),
Lead(FirstName,LastName)];
return obj;
}
}
it's probably because every record in Salesforce is natively represented as an sObject in Apex, not just 'Object'.
The rest of the code seems OK. :-)
The below code is working absolutely fine.
public class ContactAndLeadSearch {
public static List<list<sObject>> searchContactsAndLeads (String First){
List<list<sObject>> searchdata = new List<list<sObject>>();
searchdata =[Find :First IN ALL Fields RETURNING Contact(Firstname,Lastname),Lead(Firstname,Lastname)];
system.debug('@@@@'+searchdata);
Return searchdata;
}
}
public static List<List<SObject>> searchContactsAndLeads(string param)
{
List<List<Sobject>> searchConLead = [FIND: param IN all fields returning Lead(FirstName, LastName),Contact(FirstName,LastName)];
return (searchConLead);
}
}
/*
before calling this method, execute below through anonymous window first
Account acct = new Account (
Name = 'Smith Computing',
Phone = '(415 555-1212',
NumberOfEmployees = 50,
BillingCity = 'San Francisco');
insert acct;
ID acctID = acct.ID;
Contact con = new Contact (
FirstName = 'Carol',
LastName = 'Smith',
Phone = '(415) 555-1212',
Department = 'Wingo',
AccountId = acctID);
insert con;
Lead theLead = new Lead(
FirstName = 'Jim',
LastName = 'Smith',
Phone = '(111) 111-1111',
Company = 'IGATE',
LeadSource = 'Web');
insert theLead;
*/
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(string userVariable){
List<List<SObject>> result= [FIND :userVariable IN ALL FIELDS Returning Contact(FirstName,LastName), Lead(FirstName,LastName)];
return result;
}
}
FirstName = 'Brian',
LastName = 'Dent',
Phone = '(619) 852-4569',
Department = 'Mission Control',
Title = 'Mission Specialist - Neptune',
Email = 'briandent@trailhead.com');
insert thisContact;
List<List<sObject>> searchList = [FIND 'Mission Control' IN ALL FIELDS
RETURNING Contact(FirstName, LastName,
Phone, Email, Description)];
Contact[] searchContacts = (Contact[])searchList[0];
System.debug('Found the following contacts:');
for (Contact c : searchContacts) {
System.debug(c.LastName + ', ' + c.FirstName);
}
Contact[] Contacts = [FIND {Mission Control} in all fields RETURNING Contact(FirstName, LastName)];
String theseContacts = Contacts.FirstName + ' ' + Contacts.LastName;
System.debug(theseContacts);