0

I'm relatively new to Salesforce development and am having trouble with a Visualforce/Apex Class page.

The page pretty much works, except that I have a filter which should sort data by date. However, for some reason it only refreshs when I select the date I want, then choose another date. It will then bring up the correct selection.

Here's my code below:

Visualforce:

<apex:page showHeader="true" sidebar="false" controller="TCController">
    <style type="text/css">
        .red {
            background-color: red;
        }

        .amber {
            background-color: yellow;
        }

        .green {
            background-color: green;
        }

        .button {
            appearance: button;
            -moz-appearance: button;
            -webkit-appearance: button;
            text-decoration: none; font: menu; color: ButtonText;
            display: inline-block; padding: 2px 8px;
            text-align: center;
            cursor: pointer;
            margin-bottom: 5px;
        }
    </style>
    <apex:form >
        <apex:actionFunction name="" action="{!refreshTerms}" rerender="container" />
        <apex:outputPanel id="container">
            <table>
                <tr>
                    <td>
                        <strong>Date Range:</strong>
                    </td>
                    <td>
                        <apex:selectList id="dateRange" title="Date Range" value="{!selectedDateRange}">
                            <apex:selectOptions value="{!dateRange}" />
                            <apex:actionSupport event="onchange" reRender="container" />
                        </apex:selectList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a class="button" onclick="TCController()">Update</a>
                    </td>
                </tr>
            </table>
            <c:PageBlockTableEnhancerADV targetPbTableIds="tc-data" pageSizeOptions="20,50,100,200" defaultPageSize="20" enableExport="false" />
            <apex:pageBlock >
                <apex:pageBlockTable value="{!terms}" var="i" id="tc-data">
                    <apex:column headerValue="Terms Record" title="Terms Record">
                        <a href="/{!i.Id}">{!i.Name}</a>
                    </apex:column>
                    <apex:column headerValue="Company" title="Company">
                        <a href="/{!i.Company__c}">{!i.Company_Name__c}</a>
                    </apex:column>
                    <apex:column headerValue="Terms of Payment" title="Terms of Payment">
                        {!i.Terms_of_payment__c}
                    </apex:column>
                    <apex:column headerValue="Up to £19,999 fee" title="Up to £19,999 fee" styleClass="{!IF(i.Up_to_19_999_fee__c != null && i.Up_to_19_999_fee__c < 11, 'red', IF(i.Up_to_19_999_fee__c != null && i.Up_to_19_999_fee__c >=11 && i.Up_to_19_999_fee__c <=14, 'amber', IF(i.Up_to_19_999_fee__c != null && i.Up_to_19_999_fee__c > 14, 'green', NULL)))}">
                        {!i.Up_to_19_999_fee__c}
                    </apex:column>
                    <apex:column headerValue="£20,000 to £24,999 fee" title="£20,000 to £24,999 fee" styleClass="{!IF(i.X20_000_to_24_999_fee__c != null && i.X20_000_to_24_999_fee__c < 11, 'red', IF(i.X20_000_to_24_999_fee__c != null && i.X20_000_to_24_999_fee__c >=11 && i.X20_000_to_24_999_fee__c <=14, 'amber', IF(i.X20_000_to_24_999_fee__c != null && i.X20_000_to_24_999_fee__c > 14, 'green', NULL)))}">
                        {!i.X20_000_to_24_999_fee__c}
                    </apex:column>
                    <apex:column headerValue="£25,000 to £39,999 fee" title="£25,000 to £39,999 fee" styleClass="{!IF(i.X25_000_to_39_999_fee__c != null && i.X25_000_to_39_999_fee__c < 11, 'red', IF(i.X25_000_to_39_999_fee__c != null && i.X25_000_to_39_999_fee__c >=11 && i.X25_000_to_39_999_fee__c <=14, 'amber', IF(i.X25_000_to_39_999_fee__c != null && i.X25_000_to_39_999_fee__c > 14, 'green', NULL)))}">
                        {!i.X25_000_to_39_999_fee__c}
                    </apex:column>
                    <apex:column headerValue="In excess of £40,000 fee" title="In excess of £40,000 fee" styleClass="{!IF(i.In_excess_of_40_000_fee__c != null && i.In_excess_of_40_000_fee__c < 11, 'red', IF(i.In_excess_of_40_000_fee__c != null && i.In_excess_of_40_000_fee__c >=11 && i.In_excess_of_40_000_fee__c <=14, 'amber', IF(i.In_excess_of_40_000_fee__c != null && i.In_excess_of_40_000_fee__c > 14, 'green', NULL)))}">
                        {!i.In_excess_of_40_000_fee__c}
                    </apex:column>
                    <apex:column headerValue="Minimum Fee" title="Minimum Fee">
                        {!i.Minimum_Fee__c}
                    </apex:column>
                    <apex:column headerValue="Additional Comments" title="Additional Comments">
                        {!i.Additional_Comments__c}
                    </apex:column>
                    <apex:column headerValue="Have Rebates been Approved" title="Have Rebates been Approved">
                        {!i.Have_rebates_been_agreed__c}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>

Apex Class:

public class TCController {
    //Define what's in the date list.
    public List<SelectOption> getDateRange() {
        List<SelectOption> dates = new List<SelectOption>();

        dates.add(new SelectOption('Week', 'Week'));
        dates.add(new SelectOption('Month', 'Month'));
        dates.add(new SelectOption('Fiscal Quarter', 'Fiscal Quarter'));
        dates.add(new SelectOption('Fiscal Year', 'Fiscal Year'));

        return dates;
    }

    //Set the default date range.
    public String selectedDateRange {
        get {
            if (selectedDateRange == null) {
                selectedDateRange = 'Week';
            } else if (selectedDateRange == 'Week') {
                terms = [SELECT Id, Name, Company__c, Company_Name__c, Terms_of_payment__c, Up_to_19_999_fee__c, X20_000_to_24_999_fee__c,
                X25_000_to_39_999_fee__c, In_excess_of_40_000_fee__c, Minimum_Fee__c,
                Additional_Comments__c, Have_rebates_been_agreed__c FROM
                Terms_Conditions__c WHERE CreatedDate = THIS_WEEK];
            }

            else if (selectedDateRange == 'Month') {
                terms = [SELECT Id, Name, Company__c, Company_Name__c, Terms_of_payment__c, Up_to_19_999_fee__c, X20_000_to_24_999_fee__c,
                X25_000_to_39_999_fee__c, In_excess_of_40_000_fee__c, Minimum_Fee__c,
                Additional_Comments__c, Have_rebates_been_agreed__c FROM
                Terms_Conditions__c WHERE CreatedDate = THIS_MONTH];
            }

            else if (selectedDateRange == 'Fiscal Quarter') {
                terms = [SELECT Id, Name, Company__c, Company_Name__c, Terms_of_payment__c, Up_to_19_999_fee__c, X20_000_to_24_999_fee__c,
                X25_000_to_39_999_fee__c, In_excess_of_40_000_fee__c, Minimum_Fee__c,
                Additional_Comments__c, Have_rebates_been_agreed__c FROM
                Terms_Conditions__c WHERE CreatedDate = THIS_FISCAL_QUARTER];
            }

            else if (selectedDateRange == 'Fiscal Year') {
                terms = [SELECT Id, Name, Company__c, Company_Name__c, Terms_of_payment__c, Up_to_19_999_fee__c, X20_000_to_24_999_fee__c,
                X25_000_to_39_999_fee__c, In_excess_of_40_000_fee__c, Minimum_Fee__c,
                Additional_Comments__c, Have_rebates_been_agreed__c FROM
                Terms_Conditions__c WHERE CreatedDate = THIS_FISCAL_YEAR];
            }
            return selectedDateRange;
        }
        set;
    }

    //Get the information from the list object and set it.
    public List<Terms_Conditions__c> terms {
        get;
        set;
    }

    public void refreshTerms() {
        terms = new List<Terms_Conditions__c>();
    }

    //Call the constructor (this initialises everything)
    public TCController() {
        this.refreshTerms();
    }
}

Any clue to what it is I'm doing wrong would be massively appreciated!

Thank you.

1
  • Currently, you are populating the terms list in the getter of selectedDateRange. This does not really make sense to me. Put the queries in the getter of the terms variable. It'll probably work if you do it like that. Jul 21, 2014 at 11:23

1 Answer 1

1

The reason that it doesn't refresh as you expect is because the order of execution of getters is undefined. When the VF page refreshes, it appears that the getter on terms executes before the getter on selectedDateRange.

@Lex has the solution. You need to restructure your code as follows (I simplified so as not to force the reader to scroll.) Because selectedDateRange defaults to Week, terms will always return a valid list and you've removed getter order dependency.

//Set the default date range.
public String selectedDateRange {
    get {
        if (selectedDateRange == null) 
            selectedDateRange = 'Week';
        return selectedDateRange;
    }
    set;
}

//Create the list based on the filter
public List<Terms_Conditions__c> terms {
    get {
       if (this.selectedDateRange == 'Week') 
            terms = [SELECT Id, ... FROM Terms_Conditions__c WHERE CreatedDate = THIS_WEEK];
        else if (this.selectedDateRange == 'Month') 
            terms = [SELECT Id, ... FROM Terms_Conditions__c WHERE CreatedDate = THIS_MONTH];
        else if (this.selectedDateRange == 'Fiscal Quarter') 
            terms = [SELECT Id, ... FROM Terms_Conditions__c WHERE CreatedDate = THIS_FISCAL_QUARTER];
        else if (this.selectedDateRange == 'Fiscal Year') 
            terms = [SELECT Id, ... FROM Terms_Conditions__c WHERE CreatedDate = THIS_FISCAL_YEAR];
        return terms;
    }
    private set;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .