You need to sign in to do that
Don't have an account?
Trailhead SOSL module of apex class badge
Hi Friends,
I'm finishing up the Apex Basics Badge (yay!) and want to follow the recommended syntax. There's another answer posted on here that passes the challenge but uses syntax that is different than what is taught in the documentation. I really want to understand where I'm going wrong as I think I'm pretty close. Thanks for your feedback!
Here's my code:
public class ContactAndLeadSearch {
public static void List<List<sObject>> searchContactsAndLeads = [FIND 'Smith' IN NAME FIELDS RETURNING
Lead (FirstName,LastName),Contact(FirstName,LastName)];
Lead[] searchLeads = (Lead[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('Found the following Lead.');
for (Lead l : searchLeads) {
System.debug(l.FirstName);
}
System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
System.debug(c.LastName + ', ' + c.FirstName);
}
}
Here's the challenge:
Create an Apex class that returns both contacts and leads based on a parameter.
With SOSL you can search against different object types that may have similar data, such as contacts and leads. To pass this challenge, create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.
My questions: I'm not sure how to best invoke the public static method and still have the return type 'List..' Thanks for your help!
I'm finishing up the Apex Basics Badge (yay!) and want to follow the recommended syntax. There's another answer posted on here that passes the challenge but uses syntax that is different than what is taught in the documentation. I really want to understand where I'm going wrong as I think I'm pretty close. Thanks for your feedback!
Here's my code:
public class ContactAndLeadSearch {
public static void List<List<sObject>> searchContactsAndLeads = [FIND 'Smith' IN NAME FIELDS RETURNING
Lead (FirstName,LastName),Contact(FirstName,LastName)];
Lead[] searchLeads = (Lead[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('Found the following Lead.');
for (Lead l : searchLeads) {
System.debug(l.FirstName);
}
System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
System.debug(c.LastName + ', ' + c.FirstName);
}
}
Here's the challenge:
Create an Apex class that returns both contacts and leads based on a parameter.
With SOSL you can search against different object types that may have similar data, such as contacts and leads. To pass this challenge, create an Apex class that returns both contacts and leads that have first or last name matching the incoming parameter.
- The Apex class must be called 'ContactAndLeadSearch' and be in the public scope.
- The Apex class must have a public static method called 'searchContactsAndLeads'.
- Because SOSL indexes data for searching, you must create a Contact record and Lead record before checking this challenge. Both records must have the last name 'Smith'. The challenge uses these records for the SOSL search.
- The return type for 'searchContactsAndLeads' must be 'List<List< SObject>>'
- The 'searchContactsAndLeads' method must accept an incoming string as a parameter, find any contact or lead that matches the string as part of either the first or last name and then return those records.
My questions: I'm not sure how to best invoke the public static method and still have the return type 'List..' Thanks for your help!
The error your getting right now is because your puting the word "void" before List. This is because void means that the method has no return value. So when you say public static void List<List<sObject>> searchContactsAndLeads it doesn't understand if its a void method that has no return value or a list method that returns a list.
Also you are not declaring the method in your code, here is how your code should look:
Not sure is going to make you pass but i hope i helped!
public class ContactAndLeadSearch {
//Declare the method searchContactsandLeads with a return type of list
public static List<List<sObject>> searchContactsAndLeads(){
//Declare the List<List<sObject>> variable
List<List<sObject>> searchList = [FIND 'Smith' IN NAME FIELDS RETURNING
Lead (FirstName,LastName),Contact(FirstName,LastName)];
Lead[] searchLeads = (Lead[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('Found the following Lead.');
for (Lead l : searchLeads) {
System.debug(l.FirstName);
}
System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
System.debug(c.LastName + ', ' + c.FirstName);
}
//return something
return searchList;
}
}
This is what I used to complete the challenge...
public class ContactAndLeadSearch {
public static List<List<SObject>> searchContactsAndLeads(String name) {
List<List<SObject>> result= [FIND :name IN ALL FIELDS
RETURNING Contact(LastName), Lead(LastName)];
return result;
}
}
1) https://developer.salesforce.com/forums/?id=906F0000000BTk4IAG
2) https://developer.salesforce.com/forums/?id=906F0000000BO5rIAG
NOTE:- if you want to search same keyword in mutliple field then dnt add where you can try IN ALL FIELDS.
Execute below code in In Debug Annonymous window