8

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.

  • Does this work if you omit the rerender attribute? Also, is it possible that the page is refreshing, but the underlying data in the results variable has not changed? One way to tell that would be to echo {!FilterId} in the case_table and see if that changes. – Jeremy Nottingham Oct 2 '12 at 16:04
  • My comment is assuming that you have some kind of logic that re-queries or otherwise modifies the results variable. Is there anything that happens during the results get? – Jeremy Nottingham Oct 2 '12 at 16:07
  • Deleted my answer, as it wasn't helpful. Try giving the pageblock an id and rerendering that. Otherwise, look at this example of using a button - salesforce.com/us/developer/docs/pages/Content/… – metadaddy Oct 2 '12 at 20:41
4

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.

  • I would agree, it seems that you're not using the filterId set by the Ajax call to filter the list of cases returned to the page. I also agree with @metadaddy that refreshing pageBlocks is more reliable. Alternatively wrap the table in an apex:outputPanel and reRender the panel. – techtrekker Oct 2 '12 at 18:44
2

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

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

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.