1

I have a page that is trying to re-render a certain area of the page when a picklist (ndaDetails.NDA_Type__c) is changed. The picklist itself is a part of the pageBlockSection that is supposed to be re-rendered. Upon re-render, other input fields should be rendered depending on the value that was selected for the picklist.

The problem is that when I try to select a value in the picklist, it just changes back to the default value, "--None--", so I lose the value I selected AND the section doesn't re-render appropriately.

I thought the action region would take care of this, but perhaps there is something going on with the value not being sent to the controller in time for it to be saved? If so, how do I force that to happen?

VF Page:

<apex:pageBlockSection columns="1" id="toRerender">

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Test Label"/>
                <apex:actionRegion >
                    <apex:inputField value="{!ndaDetails.NDA_Type__c}" required="true">
                            <apex:actionSupport event="onchange" reRender="toRerender"/>
                    </apex:inputField>
                </apex:actionRegion>
            </apex:pageBlockSectionItem>

            <apex:outputLabel rendered="{!OR(ndaDetails.NDA_Type__c == 'discloses', 
             ndaDetails.NDA_Type__c == 'mutual')}"> 
                Test output label
            </apex:outputLabel>               

            <apex:inputField value="{!ndaDetails.Data_Classification__c}" 
            rendered="{!OR(ndaDetails.NDA_Type__c == 'disclose', 
            ndaDetails.NDA_Type__c == 'mutual')}"/>

            <apex:inputField value="{!ndaDetails.Personal_Information_Released__c}" 
            rendered="{!AND(OR(ndaDetails.Data_Classification__c == 'confidential', 
            ndaDetails.Data_Classification__c == 'restricted'), 
            OR(ndaDetails.NDA_Type__c == 'disclose', 
            ndaDetails.NDA_Type__c == 'mutual'))}"/>

</apex:pageBlockSection>

Controller:

public NDA_Details__c ndaDetails = new NDA_Details__c();
public NDA_Details__C getNdaDetails() {
    return ndaDetails;
}

Thanks in advance!

1 Answer 1

0

In practice, this design (using a variable with a getter/setter) tends to produce undesirable results with action regions. This is because Visualforce doesn't know that the variable should be processed, since it is outside the action region (it is only indirectly referenced via the getter).

Instead, use the variable directly, and initialize it in the page's constructor.

public NDA_Details__c ndaDetails { get; set; }
public ClassName() {
  ndaDetails = new NDA_Details__c();
}

This should permit the action region to see the correct data. However, in order to make sure the other areas re-render, you must also use an extra attribute that allows areas outside the region to be rendered. Without this attribute, only the area inside the region can be updated. This is a performance benefit, and is the default behavior. You can change that with:

<apex:actionRegion renderRegionOnly="false">
4
  • I'm mystified by the last code fragment as I've never had to do this (and doesn't appear in the VF big example. But then again, perhaps it explains why I always had trouble with narrow-scoped actionRegions :-)
    – cropredy
    Commented Jul 17, 2019 at 4:59
  • @cropredy Yeah, I only know about this from the numerous times odd behavior like this has happened to me.
    – sfdcfox
    Commented Jul 17, 2019 at 5:16
  • Thanks so much! I didn't realize that you could still initialize it in another place. I thought that line was taking care of the initialization, getter and setter all at one time because I'm not initializing it in a constructor, but rather in the function that redirects to this page from a different page. Commented Jul 17, 2019 at 12:12
  • However, I didn't add in that renderRegionOnly="false" and it still worked for me. Since the other fields are located within the same pageBlockSection that is being re-rendered, this extra attribute isn't necessary if I'm not mistaken. Correct me if I'm wrong. Commented Jul 17, 2019 at 12:13

You must log in to answer this question.

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