6

I have been studying Salesforce Dev for the past few weeks and can't find any documentation on something very simple. Can anyone explain why I would make the public static method a type of list rather then just having the list inside of the method? Why wouldn't I simply have the list inside of a non-list method and then when I call 'return', the method will return a list.

public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContactsAndLeads(String a) {
        list<list<sObject>> stringy = [find :a IN ALL FIELDS Returning lead(FirstName, LastName), contact(firstname, lastname)];
    return stringy;    
    }
}

2 Answers 2

11

Here are the components of that method declaration:

  1. Access Modifier - public
  2. Static vs. Instance - static
  3. Return Type - List<List<SObject>>
  4. Method Name - searchContactsAndLeads
  5. Parameters - String a

  1. You would mark this method public if you intend for it to be accessible from another Apex Class or Apex Trigger. See Access Modifiers.

  1. You would mark this method static because you can easily pass its entire state (just one String variable) in via method parameters. See Static and Instance Methods, Variables, and Initialization Code.

  1. Your return type is List<List<SObject>> because that is what SOSL itself returns. Your posted query would return a List<List<SObject>> containing one List<Lead> and one List<Contact>. You must specify a return type when defining methods, otherwise the compiler will interpret them as a constructor.

  1. The method name is probably overly verbose. The class name too. If I were writing this class, I might name it ContactsAndLeads, then just name the method itself search.

  1. The parameter your method accepts is of the type String. It could probably also be renamed to be more informative, perhaps to searchTerm.
4
  • Sorry, I still don't quite understand. I must be missing some core concepts. I understood that List<List<SObject>> was needed because I am making a list of leads and a list of contacts. So two lists there for my SOQL query. I still don't understand why I would need the: public static List<List<SObject>>.... It just seems redundant to me. Why can't it be: public class ContactAndLeadSearch { public static searchContactsAndLeads(String a) { list<list<sObject>> stringy = [find :a IN ALL FIELDS Returning lead(FirstName, LastName), contact(firstname, lastname)];
    – Olivia
    Jun 30, 2016 at 22:28
  • idea is that signature of the method should contain type that is being returned in Apex
    – kurunve
    Jun 30, 2016 at 22:37
  • 1
    If you do not define a return type, the compiler interprets your method as a constructor.
    – Adrian Larson
    Jun 30, 2016 at 22:40
  • Thank you for the break down! I understand now. The constructor part was what I was missing.
    – Olivia
    Jun 30, 2016 at 23:01
-2

List<List< sObject>> myVar = [FIND :STR IN NAME FIELDS RETURNING Contact(id), Lead(id)];

New contributor
Maksym Radchenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Is this an answer? Why? Sorry, but what's the point of this? yesterday

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .