1

I have 3 selectLists. A, B, and C. B is dependent on A, and C is dependent on B.

When selectList A changes, I rerender select list B, and C.

<apex:selectList id='listAId' value="{!valueListA}">
    <apex:selectOptions value="{!OptionsForPicklistA}" />
    <apex:actionSupport event="onchange" reRender="rerenderListB,rerenderListC" status="fetchStatus" action="{!onchangeListAAction}"/>
</apex:selectList>

<apex:selectList id="listBId" value="{!valueListB}"> 
    <apex:selectOptions value="{!OptionsForPicklistB}" />
    <apex:actionSupport event="onchange" reRender="rerenderListC" status="fetchStatus" action="{!onchangeListBAction}"/>
</apex:selectList>

<apex:selectList id="listBId" value="{!valueListC}"> 
    <apex:selectOptions value="{!OptionsForPicklistC}" />
    <apex:actionSupport event="onchange" action="{!onchangeListCAction}"/>
</apex:selectList>

In the controller the select list options are generated for selectList B by looking at what selectList A is set to. This works great. The problem is selectList C. It is supposed to be set based on what selectList B is, but it is coming in wrong. Namely off by 1, getting set based on what selectList B was set to.

What I want to have happen is this.

  1. onchange to selectList A.
  2. set value A
  3. rerender selectList B based on A.
  4. set value B
  5. rerender selectList C based on B.

What I think is happening is this:

  1. onchange to selectList A.
  2. set value A.
  3. rerender selectList B.
  4. rerender selectList C based on B. <-- rerender happens before value is set.
  5. set value B.

How would I avoid this? I am sure I am making a rookie mistake, but for the life of my, can't find it. Have looked extensively, but I can't find an example of setting an Apex value directly from a Visualforce page, nor can I find an example of calling a rerender on a Visualforce component from an Apex page.

Simplified controller looks like this:

public String valueListA {get; set;}
public String valueListB {get; set;}
public String valueListC {get; set;}

public List<SelectOption> getOptionsForPicklistA() {
    List<SelectOption> bunchOfOptions = new List<SelectOption>();

    for (getListOfAOptionsFromSOQL) {
        availObjects.add(new SelectOption(name, value));
    }

    return bunchOfOptions;
}

public List<SelectOption> getOptionsForPicklistB() {
    List<SelectOption> bunchOfOptions = new List<SelectOption>();

    for (lookUpStuffBasedOn(valueListA)) {
        availObjects.add(new SelectOption(name, value));
    }

    return bunchOfOptions;
}

public List<SelectOption> getOptionsForPicklistC() {
    List<SelectOption> bunchOfOptions = new List<SelectOption>();

    for (lookUpStuffBasedOn(valueListB)) {
        availObjects.add(new SelectOption(name, value));
    }

    return bunchOfOptions;
}
  • 1) note that in VF, the order of execution for getters is unpredictable and otherwise not defined - if you look at your debug log, you can verify this; and 2) you should post the VF snippet for the two other picklists – cropredy Dec 10 '14 at 0:57
  • @crop1645 Is the order of execution for setters is unpredictable as well? I posted the other VF snippets. Thanks. – Joshua Dance Dec 10 '14 at 17:05
  • 1
    setter order is not defined - see salesforce.stackexchange.com/a/37132/2602 – cropredy Dec 10 '14 at 17:20
3

OK, one way out of this is to surround each apex:selectList with apex:actionRegion components. (That is, three different and non-overlapping actionRegions)

Upon the onChange action, only the apex components inside the enclosing actionRegion are transmitted to the server (hence, only one setter will be invoked - the one for the selectList being changed)

By way of education, it is well worth reading the Visualforce Development Guide section on 'Order of Execution'. It is also worth examining the debug log when you initiate an action on the page - this can help associate the log events with the aforementioned documentation

Another excellent resource is VF in Practice - chapter 1 on View State is fundamental knowledge.

And finally, this helped me (and continues to help me) - Visualforce Development Cookbook by noted SFSE expert Keir Bowden - aka Bob Buzzard. Chapter 3 'Breaking up forms with action regions', pg 88 is relevant to you but the book is chock-o-block with recipes on common VF issues.

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.