[SalesForce] Action Function re-render not working

I have a selectlist on VF page as shown below. It is populated by a "getCustomerList()" method in the controller that queries the database:

<apex:selectList id="custList" onChange="recalculateProducts()" size="1" value="{!cs.Customer_Name__c}">
<apex:selectOptions value="{!CustomerList}"/>
</apex:selectList>

and a second componenet that I would like to re-render when the first one is changed or a new value is selected. It is populated by a getProductTypes() method in the controller:

<apex:selectList id="productList" size="1" value="{!cs.Product_Type__c}">
<apex:selectOptions value="{!ProductTypes}"/>
</apex:selectList>

I have an action function that calls the getProductTypes() method again onChange of the customer SelectList:

<apex:actionFunction id="refreshProducts" action="{!recalculateProducts}"  name="refreshProducts" rerender="trxList"/>

Below is the script for that action function:

<script type = "text/javascript">

    function recalculateProducts (){

        refreshProducts();

                    }

</script>   

Here are the two methods from the controller, the second one is the one I would like to run onChange:

public Set<SelectOption> getProductTypes(){

    Set<SelectOption> ProductTypes = new Set<SelectOption>();
        ProducttTypes.add(New SelectOption('Not Product Related', 'Not Product Related')); 

    String chosenCustomer = '';

    if (cs.Customer_Name__c != 'Not Product Related'){
        chosenType = 'option1';
    }
    else{
        chosenType = 'option2';
    }
    ProductTypes.add(new SelectOption(chosenType, chosenType));

    return ProductTypes;
}   

public PageReference refreshProducts (){
    getProductTypes();
    PageReference ProductRefresh = ApexPages.currentPage();
        return ProductRefresh;


}

The problem here is that the function does not seem to read cs.Customer_Name__c, and my action function does not run. No matter what I select, even if it is "Not Product Related," the page does not refresh and "option1" is always the option to select. How can I get my action function to call the recalcualteProducts javascript function and get that to call the method in the controller?

Would appreciate any help.

Best Answer

You have the function call slightly mixed up. Your actionfunction works like this:

<apex:actionFunction id="refreshProducts" action="{!recalculateProducts}"  name="refreshProducts" rerender="trxList"/>

The action refers to the Apex controller method that you are trying to call. The name is the javascript name that you call to invoke this.

So to trigger the actionfunction, you simply call refreshProducts()

ie, change this line onChange="recalculateProducts()" to this onchange="refreshProducts()"

Other things to try: Change this:

PageReference ProductRefresh = ApexPages.currentPage();
return ProductRefresh;

To this:

return null;

Finally, try using a explicit variable to hold your products rather than a getter method. ie:

public productOptions Set<SelectOption> {get;set;}
//in the constructor, assign product options a value derived from your getter method, which you can rename from getProductTypes to calcProductTypes
//in the refreshProduct method, refresh the list like this:
productOptions = calcProductTypes();