+ Start a Discussion
Charan MandapatiCharan Mandapati 

Trouble with Apex Basics & Database challenge final task "Write SOSL Query"

The challenge is 

The Apex class must be called ContactAndLeadSearch and be in the public scope
The Apex class must have a public static method called searchContactsAndLeads
The method must accept an incoming string as a parameter
The method should then find any contact or lead that matches the string as part of either the first or last name
The method should finally use a return type of List<List< SObject>>
NOTE: 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

I have written the followig code for ContactAndLeadSearch class
public class ContactAndLeadSearch {
    public static List<List<SObject>> searchContacctsAndLeads(String searchword) {
        List<List<SObject>> searchLead = [Find :searchword IN ALL FIELDS RETURNING
                                         Lead(LastName), Contact(LastName)];
        return searchLead;
    }

}

I have alos created contacts in lead and contacts with last name Smith.
But when checking the challenge I get the error "Executing the 'searchContactsAndLeads' method failed. Either the method does not exist, is not static, or does not return the expected search results."

The log file generated is
30.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,FINEST;DB,FINEST;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
Execute Anonymous: List<Contact> contacts = [SELECT ID from Contact WHERE LastName = 'Smith']; List<Lead> leads = [SELECT ID from Lead where LastName='Smith']; System.assertNotEquals(contacts.size(),0); System.assertNotEquals(leads.size(),0);
01:21:51.2 (2142431)|USER_INFO|[EXTERNAL]|0052v00000bvR7u|mandapaticharan@brave-panda-rpsvz.com|Pacific Standard Time|GMT-07:00
01:21:51.2 (2170579)|EXECUTION_STARTED
01:21:51.2 (2176878)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
01:21:51.2 (2401411)|VARIABLE_SCOPE_BEGIN|[1]|contacts|List<Contact>|true|false
01:21:51.2 (2463659)|VARIABLE_SCOPE_BEGIN|[1]|leads|List<Lead>|true|false
01:21:51.2 (2582941)|HEAP_ALLOCATE|[79]|Bytes:3
01:21:51.2 (2613502)|HEAP_ALLOCATE|[84]|Bytes:152
01:21:51.2 (2628506)|HEAP_ALLOCATE|[399]|Bytes:408
01:21:51.2 (2642831)|HEAP_ALLOCATE|[412]|Bytes:408
01:21:51.2 (2655370)|HEAP_ALLOCATE|[520]|Bytes:48
01:21:51.2 (2680056)|HEAP_ALLOCATE|[139]|Bytes:6
01:21:51.2 (2710360)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1
01:21:51.2 (2843783)|STATEMENT_EXECUTE|[1]
01:21:51.2 (2848696)|STATEMENT_EXECUTE|[1]
01:21:51.2 (2859120)|HEAP_ALLOCATE|[1]|Bytes:47
01:21:51.2 (2882649)|HEAP_ALLOCATE|[1]|Bytes:4
01:21:51.2 (2919050)|HEAP_ALLOCATE|[52]|Bytes:5
01:21:51.2 (2943444)|HEAP_ALLOCATE|[58]|Bytes:5
01:21:51.2 (2958073)|HEAP_ALLOCATE|[66]|Bytes:7
01:21:51.2 (3207639)|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|SELECT ID FROM Contact WHERE LastName = 'Smith'
01:21:51.2 (9582402)|SOQL_EXECUTE_END|[1]|Rows:1
01:21:51.2 (9620070)|HEAP_ALLOCATE|[1]|Bytes:8
01:21:51.2 (9636190)|HEAP_ALLOCATE|[1]|Bytes:29
01:21:51.2 (9671720)|HEAP_ALLOCATE|[1]|Bytes:8
01:21:51.2 (9740848)|VARIABLE_ASSIGNMENT|[1]|contacts|[{"Id":"0032v00002z0iEOAAY"}]|0x3503b13c
01:21:51.2 (9752499)|STATEMENT_EXECUTE|[1]
01:21:51.2 (9757295)|HEAP_ALLOCATE|[1]|Bytes:44
01:21:51.2 (9774642)|HEAP_ALLOCATE|[1]|Bytes:4
01:21:51.2 (10056582)|SOQL_EXECUTE_BEGIN|[1]|Aggregations:0|SELECT ID FROM Lead WHERE LastName = 'Smith'
01:21:51.2 (16181160)|SOQL_EXECUTE_END|[1]|Rows:2
01:21:51.2 (16239134)|HEAP_ALLOCATE|[1]|Bytes:12
01:21:51.2 (16266868)|HEAP_ALLOCATE|[1]|Bytes:58
01:21:51.2 (16326773)|HEAP_ALLOCATE|[1]|Bytes:12
01:21:51.2 (16383323)|VARIABLE_ASSIGNMENT|[1]|leads|[{"Id":"00Q2v00001WRJc3EAH"},{"Id":"00Q2v00001XrjPTEAZ"}]|0x71bff914
01:21:51.2 (16395197)|STATEMENT_EXECUTE|[1]
01:21:51.2 (16507556)|STATEMENT_EXECUTE|[1]
01:21:51.16 (16585324)|CUMULATIVE_LIMIT_USAGE
01:21:51.16 (16585324)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 3 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

01:21:51.16 (16585324)|CUMULATIVE_LIMIT_USAGE_END

01:21:51.2 (16622364)|CODE_UNIT_FINISHED|execute_anonymous_apex
01:21:51.2 (18459022)|EXECUTION_FINISHED

Can some one tell me where I am doing it wrong?
Ajay K DubediAjay K Dubedi
Hi Charan,

Click on this link helpful for you.

https://developer.salesforce.com/forums/?id=906F0000000BTk4IAG

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com