Filtering reports through Apex and Visualforce (Salesforce web)

I am trying to print out a Salesforce custom object database and have a filter. The filters work correctly, but the "All" filter does not show the full table as a default or when I click "All" from another filter. How can I do this?

Here is the relevant Apex code:

public string itemPickValue{get; set;}
    public List<QC_Issue__c> issuesList{get; set;}
    public List<QC_Issue__c> results; 

    public List<QC_Issue__c> getIssues() {
        results = Database.query(
            'SELECT Component_Category__c, Issue_Name__c, Issue_Description__c, Severity_1_Criteria__c, Severity_2_Criteria__c, Severity_3_Criteria__c, File__c FROM QC_Issue__c Order By Component_Category__c, Issue_Name__c'

        );

        return results; 
    }

    public void getIssuesList() {
        if(itemPickValue.equals('All')) issuesList = Database.query('SELECT Component_Category__c, Issue_Name__c, Issue_Description__c, Severity_1_Criteria__c, Severity_2_Criteria__c, Severity_3_Criteria__c, File__c FROM QC_Issue__c');
        issuesList = Database.query('SELECT Component_Category__c, Issue_Name__c, Issue_Description__c, Severity_1_Criteria__c, Severity_2_Criteria__c, Severity_3_Criteria__c, File__c FROM QC_Issue__c WHERE Component_Category__c =: itemPickValue');
    }

And here is the relevant Visualforce code:

<apex:pageBlock title="QC Glossary" id="qc_issues_list">
                <h3>Filter by Component Category:</h3>
                <br></br>
                <!-->Start Filter and List<-->
                    <apex:selectList size="1" value="{!itemPickValue}">
                        <apex:actionSupport event="onchange" action="{!getIssuesList}" rerender="table"/>
                            <apex:selectOption itemLabel="All" itemValue="All"></apex:selectOption>
                            <apex:selectOption itemLabel="Animation/VFX" itemValue="Animation/VFX"></apex:selectOption>
                    </apex:selectList>
                    <br></br>
                    <br></br>

                    <apex:pageBlockTable id="table" value="{!issuesList}" var="qc" columnsWidth="10%, 5%, 50%, 10%, 10%, 10%, 10%">
                        <apex:column value="{!qc.Component_Category__c }"/>
                        <apex:column value="{!qc.Issue_Name__c }"/>
                        <apex:column value="{!qc.Issue_Description__c}"/>
                        <apex:column value="{!qc.Severity_1_Criteria__c}"/>
                        <apex:column value="{!qc.Severity_2_Criteria__c}"/>
                        <apex:column value="{!qc.Severity_3_Criteria__c}"/>
                        <apex:column value="{!qc.File__c}"/>
                    </apex:pageBlockTable>

1 answer

  • answered 2018-08-09 13:50 Dpolehonski

    I've made the following changes

    • Removed Code duplication, using the getIssues method rather than duplicating the SOQL statement.
    • Added an if/else clause; Your main issue was in the getIssuesList you had an if statement that if true would store the full result in issuesListand then immediatly overwrite it in the next line with a filtered list attempting to use the category name 'All'

    There are various other things to point out here; but I'll leave that for now.

    The result:

    public String itemPickValue{get; set;}
    public List<QC_Issue__c> issuesList{get; set;}
    public List<QC_Issue__c> results; 
    
    public List<QC_Issue__c> getIssues() {
        results = Database.query(
            'SELECT Component_Category__c, Issue_Name__c, Issue_Description__c, Severity_1_Criteria__c, Severity_2_Criteria__c, Severity_3_Criteria__c, File__c FROM QC_Issue__c Order By Component_Category__c, Issue_Name__c'
        );
    
        return results; 
    }
    
    public void getIssuesList() {
        if(itemPickValue.equals('All')) {
            issuesList = getIssues();
        } else {
            issuesList = Database.query('SELECT Component_Category__c, Issue_Name__c, Issue_Description__c, Severity_1_Criteria__c, Severity_2_Criteria__c, Severity_3_Criteria__c, File__c FROM QC_Issue__c WHERE Component_Category__c =: itemPickValue');
        }
    }