Find your content:

Search form

You are here

How do I get a selectList in my VF page for a custom case controller to work?

 
Share

I have a VisualForce page that uses a custom controller. This is my controller:

global class CompanyCasesQuery {

    //constructors and variable declaration

    global List<Case> results {
        get {
            //returns a list of cases

            return results;
        } 
        set; 
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('00BW0000000PeSVMA0','View All Open Cases'));
        options.add(new SelectOption('00BW0000000PbzWMAS','View All Cases'));
        options.add(new SelectOption('00BW0000000PeSGMA0','View All Closed Cases'));
        options.add(new SelectOption('00B300000005XQtEAM','Recently Viewed Cases'));
        return options;
    }

    public String[] getFilterId() {
        return filterid;
    }

    public void setFilterId(String[] filterid) {
        this.filterid = filterid;
    }
}

This is my VisualForce page:

<apex:page controller="CompanyCasesQuery" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="My Cases">
              <apex:outputLabel value="View:"/>
              <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" action="{!results}" rerender="cases_table"/>
                <apex:selectOptions value="{!items}"/>
              </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table" >
                    <apex:column >
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">Case Number</apex:facet>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

The cases show up correctly and the select list shows up correctly. However, when I select an option in my select list, nothing happens. What I would like to have happen is when I select an option in the select list, my cases refresh like the default Salesforce cases page. When I select "View All Cases", all cases are displayed, when I select "View All Closed Cases", only closed cases are displayed. How do I get this select list refresh to work? Thanks.


Attribution to: Di Zou

Possible Suggestion/Solution #1

This is what I ended up doing:

In my Controller:

global class MyCasesQuery {

    public List<Case> results {get;set;}
    public String filterId {get;set;}
    private Id accountId;

    public PageReference processRequests()
    {
        previousSortField = 'CaseNumber';
        //write the query for getting the data based on filterId
        if (filterId == 'All') {
            results = [select Id, CaseNumber, AccountId, Subject, Priority, Status, CreatedDate, ContactId from Case where AccountId = :accountId order by CaseNumber desc limit 100];
        } else {
            results = [select Id, CaseNumber, AccountId, Subject, Priority, Status, CreatedDate, ContactId from Case where Status = :filterId and AccountId = :accountId order by CaseNumber desc limit 100]; 
        }
        return null;
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Open', 'Open'));
        options.add(new SelectOption('Closed', 'Closed'));
        options.add(new SelectOption('All', 'All'));
        return options;
    }
}

My VF Page:

<apex:page controller="MyCasesQuery" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="My Cases">
         <apex:outputLabel value="View: "/>
          <apex:selectList value="{!filterId}" size="1">
           <apex:actionSupport event="onchange" action="{!processRequests}"  rerender="cases_table"/>
             <apex:selectOptions value="{!items}"/>
           </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table" >
                    <apex:column headerValue="Case Id">
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.CaseNumber.Label}
                        </apex:facet>
                    </apex:column>
                    <apex:column value="{!c.ContactId}">
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.ContactId.Label}
                        </apex:facet>
                    </apex:column>
                    <apex:column >
                        <a target="_parent" href="/{!c.id}">{!c.Subject}</a>
                        <apex:facet name="header">Subject</apex:facet>
                    </apex:column>
                    <apex:column value="{!c.Status}">
                        <apex:facet name="header">Status</apex:facet>
                    </apex:column>
                    <apex:column value="{!c.Priority}">
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.Priority.Label}
                        </apex:facet>
                    </apex:column>
                    <apex:column value="{!c.CreatedDate}">
                        <apex:facet name="header">
                            {!$ObjectType.Case.Fields.CreatedDate.Label}
                        </apex:facet>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Attribution to: Di Zou

Possible Suggestion/Solution #2

Submitting the form in the onchange event of the selectList works for me:

<apex:outputLabel value="Select view: " for="theListCheckbox"/>
<apex:selectList title="View:" value="{!filterid}" size="1"           
                 onchange="javascript:this.form.submit();" id="theListCheckbox">
    <apex:selectOptions value="{!listviewoptions}"/>
</apex:selectList>

Where you can replace the {!listviewoptions} by your own list of options


Attribution to: Jelle van Geuns

Possible Suggestion/Solution #3

You aren't performing a query in your controller. When you change the picklist you'll need to (re)issue a SOQL query, returning those cases that match whatever your filter would mean (all open, all etc).

This query would be in your getResults method and vary depending on the option picked. What happens now is that it always returns null when called because it's never assigned to anything.


Attribution to: JLiljegren__c

Possible Suggestion/Solution #4

First of I am not sure if you can get the Recently Viewed cases and all cases.

I managed to modify the code as below :

PAGE:

<apex:page controller="CompanyCasesQue" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="My Cases">
              <apex:outputLabel value="View:"/>
              <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" action="{!renderdiffview}" rerender="cases_table"/>
                <apex:selectOptions value="{!items}"/>
              </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table">
                    <apex:column >
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">Case Number</apex:facet>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
</apex:page>

controller :

global class CompanyCasesQue {
public string FilterId{get;set;}
public list<case> results;
    //constructors and variable declaration

    public PageReference renderdiffview() {
    Map<Id,string> Id_status_map = new map<Id,string>{'00BW0000000PeSVMA0' => 'New','00BW0000000PeSGMA0' => 'Closed'};
    results = [select id,casenumber from case where status =: Id_status_map.get(filterId)];
     return null;
    }

    public List<Case> getresults() {       
     return results;
    }
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('00BW0000000PeSVMA0','View All Open Cases'));
        options.add(new SelectOption('00BW0000000PbzWMAS','View All Cases'));
        options.add(new SelectOption('00BW0000000PeSGMA0','View All Closed Cases'));
        options.add(new SelectOption('00B300000005XQtEAM','Recently Viewed Cases'));
        return options;
    }
}

I have a map in the controller since the filter Id is the Id = '00BW0000000PeSVMA0' being sent from page. I mapped all open cases and closed cases to status = open and status = closed.

for all open Cases I would go a diff route and use pagination since there might be multiple cases.


Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1852

My Block Status

My Block Content