+ Start a Discussion
Aron Schor [Dev]Aron Schor [Dev] 

Creating & Using Custom Controllers Challenge problem

I have been able to get similar ones to work (based on prior challenges) but I am struggling here.  Probably a newbie mistake somewhere.  Any help would be appreciated!  Thanks, Aron

-> Page (Gives Error: Unknown property 'String.Id error)

<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:repeat value="{!Case}" var="case">
            <apex:outputLink value="{!Case.Id}">{!Case.Id}</apex:outputLink>
            <apex:outputLink value="{!case.CaseNumber}">{!case.CaseNumber}</apex:outputLink>
        </apex:repeat>      
        </apex:pageBlock>
    </apex:form>
</apex:page>

-> Class

ublic class NewCaseListController {

    public String getCase() {
        return null;
    }


    public String getCases() {
        return null;
    }

private String sortOrder = 'CaseNumber';
public List<Case> getNewCases() {
    List<Case> results = Database.query(
        'SELECT Id, CaseNumber ' +
        'FROM Case ' +
        'WHERE Status = New ' +
        'ORDER BY ' + sortOrder + ' ASC ' +
    );
    return results;
    }
}
Best Answer chosen by Aron Schor [Dev]
Mathew Andresen 5Mathew Andresen 5
I'm not quite sure why you have 3 methods (1 getCase, and 2 getCases).  Really you just need the one to return the Case object and all the detail of it.  Your other get methods aren't doing anything because they are returning null.

I'm not an expert, but I made some modifications, it seems to complile for me, but I haven't tested it, because we don't use cases.


Note, I removed your sort order because I wasn't sure what you wanted to sort by.  Ie it should be ORDER By Name ASC   etc

Also, there shouldn't be a +  after ASC  because there's nothing else there.


 
<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:repeat value="{!Case}" var="cs">
            <apex:outputLink value="{!cs.Id}">{!cs.Id}</apex:outputLink>
            <apex:outputLink value="{!cs.CaseNumber}">{!cs.CaseNumber}</apex:outputLink>
        </apex:repeat>      
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
Public class NewCaseListController {
List<Case> results = new List<Case>();


private String sortOrder = 'CaseNumber';
public List<Case> getCase() {
    
    results = [SELECT Id, CaseNumber FROM Case WHERE Status = 'New'];

    return results;
    }
}

Hope this helps.
 

All Answers

Mathew Andresen 5Mathew Andresen 5
I'm not quite sure why you have 3 methods (1 getCase, and 2 getCases).  Really you just need the one to return the Case object and all the detail of it.  Your other get methods aren't doing anything because they are returning null.

I'm not an expert, but I made some modifications, it seems to complile for me, but I haven't tested it, because we don't use cases.


Note, I removed your sort order because I wasn't sure what you wanted to sort by.  Ie it should be ORDER By Name ASC   etc

Also, there shouldn't be a +  after ASC  because there's nothing else there.


 
<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:repeat value="{!Case}" var="cs">
            <apex:outputLink value="{!cs.Id}">{!cs.Id}</apex:outputLink>
            <apex:outputLink value="{!cs.CaseNumber}">{!cs.CaseNumber}</apex:outputLink>
        </apex:repeat>      
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
Public class NewCaseListController {
List<Case> results = new List<Case>();


private String sortOrder = 'CaseNumber';
public List<Case> getCase() {
    
    results = [SELECT Id, CaseNumber FROM Case WHERE Status = 'New'];

    return results;
    }
}

Hope this helps.
 
This was selected as the best answer
Aron Schor [Dev]Aron Schor [Dev]
Thanks Mathew! I made two minor changes, see notes below, and completed this!  I made some other revisions to make the formatting better but I can't get the URLS to work.  Not sure if you can help here bu that would be great if you could.

For example, from the page case id shows 500j0000001XPSTAA4 and takes you to this link
https://c.na16.visual.force.com/apex/500j0000001XPSTAA4

Case number 0001063 takes you here
https://c.na16.visual.force.com/apex/00001063

Neither of these pages work, the The correct link 
https://na16.salesforce.com/500j0000001XPST

Let me know if you have time!

CURRENTLY HAVE:

<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:repeat value="{!NewCases}" var="case">
         <table style="width:1000px;">
<tr>
            <BR><span style="margin-left:20px"> </span><apex:outputLink value="{!case.Id}" style="width:160px">{!case.Id}</apex:outputLink>
            <span style="margin-left:20px"> </span><apex:outputLink value="{!Case.CaseNumber}" style="width:160px" >{!case.CaseNumber}</apex:outputLink></BR>
           </tr>
           </table>
        </apex:repeat>      
        </apex:pageBlock>
    </apex:form>
</apex:page>


NOTES ON MY CHANGES:

Trailhead said:
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

I changed:
Page <apex:repeat value="{!NewCases}" var="cs">
Apex public List<Case> getNewCases() {

Trailhead said"
Challenge not yet complete... here's what's wrong: 
The repeat component does not have the var attribute set correctly

I changed to
         <apex:repeat value="{!NewCases}" var="case">
         <apex:outputLink value="{!case.Id}">{!case.Id}</apex:outputLink>
         <apex:outputLink value="{!case.CaseNumber}">{!case.CaseNumber}</apex:outputLink>
Mathew Andresen 5Mathew Andresen 5
So just to be clear, you want to get the Case ID from the URL, and then pass it to the apex controller yes?

So that means the URL needs to be something like ?Id=500j0000001XPSTAA4

Then you can add in the following to get the ID into the class
Id PlanId = ApexPages.currentPage().getParameters().get('id');

And then adjust the SQL query to include the ID 
results = [SELECT Id, CaseNumber FROM Case WHERE id = :planId ];


 
Aron Schor [Dev]Aron Schor [Dev]
I either want the Case id or number to direct to the case.  Right now the URLs show a page not found error.

When I try this code the page just says Case List at the top and nothing below.  Do I need to edit the page as well?

private String sortOrder = 'CaseNumber';
public List<Case> getNewCases() {
    Id PlanId = ApexPages.currentPage().getParameters().get('id');
    results = [SELECT Id, CaseNumber FROM Case WHERE id = :planId];

    return results;
    }
}
Mathew Andresen 5Mathew Andresen 5
Yes at the end of the page URL append
?Id=500j0000001XPSTAA4
Aron Schor [Dev]Aron Schor [Dev]
Sorry for the late reply...If you are still able to let me know, I am trying to enter a general code so all the links would work, not just that one.
Tom Barad 7Tom Barad 7
Try something like this for your outputLink, where "na16" is your server.  There may be a better way to get this URL as a variable, but I'm a newbie, too.
<apex:outputLink value="https://na16.salesforce.com/{!cs.Id}">{!cs.CaseNumber}</apex:outputLink>


 
Aron Schor [Dev]Aron Schor [Dev]
Thanks Tom, I got this working from help in a different thread for a similar issue.


<apex:column headerValue="Product Code">
<apex:outputlink value="https://ap1.salesforce.com/{!a.id}">
{!a.ProductCode}
</apex:outputlink>
</apex:column>
 
Alisha GuptaAlisha Gupta
can you post the whole code. although my code is showing correct output but trailhead is showing same error as yours
Aron Schor [Dev]Aron Schor [Dev]
This is an old one but I think this is what you need!

Public class NewCaseListController {
List<Case> results = new List<Case>();


private String sortOrder = 'CaseNumber';
public List<Case> getNewCases() {
    results = [SELECT Id, CaseNumber FROM Case WHERE status = 'new'];

    return results;
    }
}


_____________________


<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:repeat value="{!NewCases}" var="case">
         <table style="width:1000px;">
<tr>
            <BR><span style="margin-left:20px"> </span><apex:outputLink value="{!case.Id}" style="width:160px">{!case.Id}</apex:outputLink>
            <span style="margin-left:20px"> </span><apex:outputLink value="{!Case.CaseNumber}" style="width:160px" >{!case.CaseNumber}</apex:outputLink></BR>
           </tr>
           </table>
        </apex:repeat>     
        </apex:pageBlock>
    </apex:form>
</apex:page>
L3nL3n
Hi, I tried to use the repeat in a pageBlockTable with columns. But the value did not show up in the table. Do you know why?
 
<apex:page controller="NewCaseListController">
    <apex:form >
        <apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:pageBlockTable value="{!NewCases}" var="case">
         <apex:column>
         <apex:repeat value="{! NewCases}" var="case">
            <apex:outputLink value="https://ap1.salesforce.com/{!case.Id}">{!case.Id}</apex:outputLink>
            
        </apex:repeat>
        </apex:column>
        <apex:column value="{! case.CaseNumber}"></apex:column>
             <apex:column value="{! case.Status}"></apex:column>
        </apex:pageBlockTable>     
        </apex:pageBlock>
    </apex:form>
</apex:page>

User-added image
Mathew Andresen 5Mathew Andresen 5
You don't need to use repeat in a pageblock table, it automatically cycles through the values on it's own.
 
<apex:pageBlock title="Case List" id="Case_list">
            <!-- Case_list -->
         <apex:pageBlockTable value="{!NewCases}" var="case">
         <apex:column>
         <apex:outputLink value="https://ap1.salesforce.com/{!case.Id}">{!case.Id}</apex:outputLink>
             </apex:column>
       <apex:column value="{! case.CaseNumber}"></apex:column>
             <apex:column value="{! case.Status}"></apex:column>
        </apex:pageBlockTable>     
        </apex:pageBlock>

 
InesGarciaInesGarcia
Alrighty,

After speding quite a bit of time on this myself.

1. Please do not hardcode your instance as this may change.
2. The repeat component needs:Value, Var and ID. 
3. Filter on New to 'WHERE Status = \'New\' '

So thats is how would work and pass the badge:

VISUALFORCE PAGE NewCaseList
<apex:page controller="NewCaseListController">
    <apex:form >

        <apex:pageBlock title="New Case List" id="cases_list">

            <apex:repeat value="{!newCases}" var="case" id="case">
                <p><apex:outputLink value="/{!case.Id}">{!case.CaseNumber}</apex:outputLink></p>
            </apex:repeat>

        </apex:pageBlock>

    </apex:form>

</apex:page>

CUSTOM CONTROLLER NewCaseListController
public class NewCaseListController {

    public List<Case> getNewCases() {
        List<Case> results = Database.query(
            'SELECT Id, CaseNumber, Status ' +
            'FROM Case ' +
            'WHERE Status = \'New\' '
        );
   
    return results;
    }

}

Tada!
praveen jha 36praveen jha 36
below code will work and help to complete the challenge 100% 

VF 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>


Controller Class:-

public class NewCaseListController {

    public  List<Case> getNewCases()
    {
        List<Case> caseList = new List<Case>();
        for(Case ct: [Select Id, CaseNumber FROM Case WHERE Status = 'New'])
            caseList.add(ct);

        return caseList;
    }
}


 
Nagendra K 6Nagendra K 6
I tried many easy and complex ways , here the code works
VF Page:
<apex:page controller="NewCaseListController" >
  <apex:pageblock title="New status Cases">
   <apex:repeat value="{!newCases}" var="case">
    <li>
    <apex:outputLink value="/{!case.id}">{!case.id} </apex:outputLink>
     {!case.casenumber}    
   </li>
   </apex:repeat>  
  </apex:pageblock>
</apex:page>
Controller:
public class NewCaseListController {
    public List<case> getNewCases()
    {    
     list<case>newcase =[select id,casenumber from case where status='new'];
     return newcase;
    }
}
Akshay KopardeAkshay Koparde
This is the code I wrote and it worked for me

Custom Controller
public class NewCaseListController {
     List <Case> NewCases =[SELECT Id, CaseNumber FROM Case WHERE Status ='New'];
     public list <Case> getNewCases()
     {
         return NewCases;
     }     
}

VisualForce Code
<apex:page controller="NewCaseListController">
<apex:form >
    <apex:pageBlock >
        <apex:repeat value="{!NewCases}" var="case">
            <apex:outputLink value="{!case.Id}">
            {!case.CaseNumber}
            </apex:outputLink>
        </apex:repeat>
    </apex:pageBlock>
</apex:form>  
</apex:page>

 
Trailhead StrangerTrailhead Stranger
Thanks Akshay Koparde, It worked for me!
Sourav H 7Sourav H 7
What is the significance of this? Code worked even without this..
private String sortOrder = 'CaseNumber';
Sourav H 7Sourav H 7
What is the significance of this? Code worked even without this..
private String sortOrder = 'CaseNumber';
ani ghoani gho
//i read all above  as the SF console id not good with help and the following which passes the challenge. Also lot of answers above are pseudocode or code tried in some other IDES not challenge tried so that will create more problems for newbeeees..... along with some copy paste from other examples which is echoed above with sortOder and all apperaring from othe egs on challenge......:) fun fun fun...
public class NewCaseListController {
    
     public list <Case> getNewCases()
     {
         return [SELECT Id, CaseNumber FROM Case WHERE Status ='New'];
     }     

}

<apex:page controller="NewCaseListController" >
      <apex:form >
        <apex:pageBlock title="New Case List" id="case_list">
            
       <apex:repeat value="{!NewCases}" var="case" id="case">
               <p><apex:outputLink value="/{!case.Id}">{!case.CaseNumber}</apex:outputLink></p>
        </apex:repeat>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Robert McLellanRobert McLellan
I kept running into this same problem. Oddly, by habit, I used public static List<Case> getNewCases() for my method. I removed static and it finally accepted my answer.