1

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.

  • update onChange="recalculateProducts()" to onChange="refreshProducts()" in <apex:selectList id="custList">. Now create recalculateProducts() on your controller. It ll work. – Saumya Ranjan Satapathy Feb 18 '16 at 17:33
  • And the <script>code</script> is not reqired. Action function is the javascript method. – Saumya Ranjan Satapathy Feb 18 '16 at 17:34
3

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();
  • it looks like this is correct in getting the action method to run, but it still appears that I cannot get it to re-render, as it is not reading the cs.Customer_Name__c field and it does not update. A couple more tries and I will mark this answer as correct, as it does fix getting the action function to run. – Chris P Feb 18 '16 at 17:37
  • try changing onChange to onchange - I believe this attribute is case sensitive – Caspar Harmer Feb 18 '16 at 17:41
  • no such luck there. It does not seem to read the onchanged" value. – Chris P Feb 18 '16 at 17:44
  • I've added some of the other things you should try. Not using an automatic getter is a good idea, as you get to control when you assign values to the select list - using an automatic getter removes that control. Calling the automatic getter method in the PageReference method doesn't help - just think - what are you assigning the result to? – Caspar Harmer Feb 18 '16 at 17:51

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.