+ Start a Discussion
Matthew StroudMatthew Stroud 

Failed Custom Controllers Trailhead with Working Code

Here is my controller:
public class NewCaseListController {
    public static List<Case> getNewCases() {
        List<Case> results = [SELECT Id, CaseNumber FROM Case WHERE Status ='New'];
        return results;
    }
}
Here is my page:
<apex:page controller="NewCaseListController">
    <apex:repeat value="{! newCases }" var="case">
        <li><apex:outputLink value="/{!case.Id}">{!case.CaseNumber}</apex:outputLink></li>
    </apex:repeat>
</apex:page>
Here is the result:
User-added image
But when I verify the challenge I get:
"Challenge not yet complete... here's what's wrong:
Case records were not returned upon calling 'getNewCases'. Either the method does not exist, or it does not return the expected list of cases."
Best Answer chosen by Matthew Stroud
Jithin Krishnan 2Jithin Krishnan 2
Hi Matthew,
Can you please try after removing static keyword from your getNewCases method? I think that is the problem.
Thanks

All Answers

Matthew StroudMatthew Stroud
There are 3 Cases in my org with STATUS New, calling the NewCaseListController.getNewCases() returns those 3 records.
Jithin Krishnan 2Jithin Krishnan 2
Hi Matthew,
Can you please try after removing static keyword from your getNewCases method? I think that is the problem.
Thanks
This was selected as the best answer
Matthew StroudMatthew Stroud
Okay that worked, but can you explain why?  Practically every method created prior to that one needed static. 
Matthew StroudMatthew Stroud
I had originally added the static modifier to make it easy to test the method in the Execute Anonymous Window from the Developer Console.  It is also curious to me that it only failed in the challenge, but works fine otherwise.  Does this have something to do with the why the challenge is validated?
Jithin Krishnan 2Jithin Krishnan 2
I think it is because thats how the challenge is validated. The code works as expected even with the static keyword.
Static methods can be invoked with the classname without having to create the object of the class. But I seriously don't know how it mattered here. I found out the problem in your code by trial and error. 
Jithin Krishnan 2Jithin Krishnan 2
Also, trailhead challenges sometimes works in a wierd way! :)