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

1 Answer 1

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
    Commented Jun 30, 2016 at 22:28
  • idea is that signature of the method should contain type that is being returned in Apex
    – kurunve
    Commented 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
    Commented Jun 30, 2016 at 22:40
  • Thank you for the break down! I understand now. The constructor part was what I was missing.
    – Olivia
    Commented Jun 30, 2016 at 23:01

You must log in to answer this question.

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