function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Diane CarrollDiane Carroll 

Help with custom controller?

Hello- I'm hoping for some help with a visualforce page.  I can't seem to find the answers in the documentation. 

What I need to do is diplay a specific subset of cases on a public visualforce page.  I need to be able to include the comments as well.  I can successfully display cases but can't quite get it to the point where it only displays the ones I need. (specific record type and 2 types)

For some reason the code (as shown at the bottom) shows right across the bottom of the screen and I just can't figure out how to add the comments in a column too. I've tried creating a custom controller, but my apex fell short of success. 

Code:
<apex:page id="GraniteChangeLog2" standardController="Case" recordSetvar="cases" sidebar="false" showHeader="false" standardStylesheets="true">
 <body style="background-image:url('{!$Resource.Logo}');" />
    <apex:define name="head">
        <title>Granite State College-Change Log v2</title>
    </apex:define>
    <apex:define name="content">
        <apex:variable var="width" value="850"/>
        <apex:variable var="border" value="0"/>
        <apex:variable var="cellspacing" value="0"/>
        <apex:variable var="cellpadding" value="5"/>
 <apex:form >  

    <apex:pageBlock >
  
    <b><h1>Change Management Cases</h1></b>
        <apex:pageBlockTable value="{!cases}" var="c">    
            <apex:column value="{!c.casenumber}"/>
            <apex:column value="{!c.CreatedDate}"/>
            <apex:column value="{!c.status}"/>
            <apex:column value="{!c.subject}"/>
            <apex:column value="{!c.type}"/>
            <apex:column value="{!c.BusinessCase__c}"/>
            <apex:column value="{!c.RiskAssesment__c}"/>
            <apex:column value="{!c.RollbackPlan__c}"/>
            <apex:column value="{!c.TestPlan__c}"/>
            <apex:column value="{!c.IsClosed}"/>
          
         </apex:pageBlockTable>
          </apex:pageBlock>

 </apex:form>
 </apex:define>
 
  public class retrieveCase {

        public List getCases() {
        return [SELECT status, subject FROM Case
                WHERE RecordTypeID='012M0000000D62J' AND status != 'Closed' limit 5];

    }


 
</apex:page>

Thank you.
Hermes Yan 11Hermes Yan 11
Your controller logic needs to be in an apex class. Create a new apex class called retrieveCase and drop the logic in there.

The apex page also needs to either implement that new apex class as an extension or you can drop the standard case controller for a custom controller. Depending on the path you choose your retrieveCase class would need appropriate changes.

Hopefully that points you in the right direction. Refer to the documentation section on extensions and custom controllers.
pconpcon
You have a couple of different options
  1. You can either use a subquery [1] and select the comments associated with your case, and add them to the layout.
  2. You can take all of you cases and make a seperate query that gets the comments, stores them in a map of Case Id => List of Comments and then using a new method return that list to be displayed.  If you go with this route, I would suggest that you move your case query to the constructor of you class and store it in a class level variable.  That way it will only be called once and you can use getCases multiple times without hitting any DML limits.
If you need more explaination of either of these two methods, please let me know.

NOTE: Please use the "Add a code sample" button (icon of <>) to increase readability

[1] http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm
Diane CarrollDiane Carroll
Custom controller:
public class displaycase {

    public String c { get; set; }

    public String cases { get; set; }
public string getcasertype{get;set;}
public  void displaycase ()
{
    
}
public list<case> caselst{get;set;}


public list<selectoption>getcasertype()
{
    list<selectoption>selectopt=new list<selectoption>();
    selectopt.add(new selectoption('Change Management','Change Management'));
        return selectopt;
}
public void displaycaselist()
{
    caselst=new list<case>();
   caselst=[SELECT CaseNumber,CreatedDate,(SELECT CommentBody,CreatedDate From CaseComments),ClosedDate,IsClosed,Priority,Reason,Description,Status,Subject,Type,BusinessCase__c,RiskAssesment__c,RollbackPlan__c,TestPlan__c FROM Case WHERE RecordType.DeveloperName='Change Management' AND Type='Web Tailor'];
}
}
As you can see, I found out how to include the case comments in my controller. 
I cannot find an answer in the thousands of pages of documentation and postings. 

Perhaps I'm asking my question incorrectly so... please point me in the right direction..I need either to find out where to include this code to use the standard controller for cases OR the correct syntax to list the columns with a custom controller.    Every sample I have found shows it exactly this way:
apex:pageBlockTable value="{!cases}" var="c">    
            <apex:column value="{!c.casenumber}"/>
but when I try this same method with my custom controller I get an error message saying "Unknown property 'String.casenumber'"
Thank you.
pconpcon
You'd want to do something more like this
 
public class displaycase {
    public String recordType {
        get;
        set {
            caseList = null;
        }
    }
    public List<Case> caseList {
        get {
            if (caseList == null) {
                caseList = [
                    select CaseNumber,
                        CreatedDate,
                        (
                            select CommentBody,
                                CreatedDate
                            from CaseComments
                        ),
                        ClosedDate,
                        IsClosed,
                        Priority,
                        Reason,
                        Description,
                        Status,
                        Subject,
                        Type,
                        BusinessCase__c,
                        RiskAssesment__c,
                        RollbackPlan__c,
                        TestPlan__c
                    from Case
                    where RecordType.DeveloperName = :recordType and
                        Type = 'Web Tailor'
                ];
            }
            return caseList;
        }
        set;
    }
    
    public List<CaseComments> caseCommentList {
        get {
            return caseList.CaseComments;
        }   
        set;
    }
    
    public displaycase() {
        this.recordType = 'Change Management'; //Setting a default
    }

    public List<SelectOption> getCaseSelectOption() {
        List<SelectOption> selectOpt = new List<SelectOption>();
        selectOpt.add(new SelectOption('Change Management','Change Management'));
        return selectOpt;
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

Then you should be able to use your VisualForce like that.  Since you were just returning a string, you cannot get the object information.

In the code above I'm doing some lazy loading [1] of the cases so that if you don't change the select option (that you would store in the recordType variable) it will only query a single time.  You will need to do some selective page reloading [2] in your VisualForce for when you change the RecordType select list, but that's a different issue all together.

[1] http://blog.deadlypenguin.com/blog/2012/04/04/reducing-salesforce-soql-queries-by-using-static-variables/
[2] http://blog.deadlypenguin.com/blog/2012/07/09/dynamic-dependent-picklists-in-salesforce/