1

I've been spending a lot more time than I thought I would trying to get this to work. On my VisualForce page I have a pick list that depending on the value will display or hide additional fields. I have looked at multiple solutions and I can't seem to get any of them to work.

My action function updateFieldVisibility doesn't seem to be being called because no debug statements are in my logs. I've tried to rerender the form, page block, and page block sections but they do not change. What am I missing here?

Visual Force Page:

<apex:page docType="html-5.0" controller="ConnectorHomeTabController" tabStyle="HigiConnector_HomePage__tab">
<apex:form id="editForm">
    <apex:pageBlock title="Edit Organization Default Data Access" id="editPageBlock">

        <apex:pageBlockButtons>
            <apex:commandButton action="{!saveDefaultSettings}" value="Save " />
            <apex:commandButton action="{!cancelEdit}" value="Cancel" />
        </apex:pageBlockButtons>

        <!-- Cadence specific fields -->
        <apex:actionRegion>
        <apex:pageBlockSection title="Cadence Settings" columns="2">

            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Preferred Cadence" for="cadence" />
                <apex:selectList value="{!selectedCadence}" size="1" id="cadence" >
                    <apex:selectOptions value="{!cadenceOptions}" />
                    <apex:actionSupport event="onChange" action="{!updateFieldVisibility}" reRender="editForm" />
                </apex:selectList> 
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem helpText="Checking this box will allow data refreshes on the weekend at 5:00 PM">
                <apex:outputLabel value="Weekend Refresh" for="weekendRefresh" />
                <apex:inputCheckbox id="weekendRefresh" />
            </apex:pageBlockSectionItem>

        </apex:pageBlockSection>

        <apex:pageBlockSection columns="1" id="additionalFields">
            <apex:pageBlockSectionItem rendered="{!isShowRefreshWeekday}">
                <apex:outputLabel value="Weekday to Refresh" for="weekdayRefresh" />
                <apex:selectList value="{!selectedWeekdayRefresh}" size="1" id="weekdayRefresh">
                    <apex:selectOptions value="{!weekdayRefreshOptions}"></apex:selectOptions>
                </apex:selectList>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem rendered="{!isShowRefreshTime}">
                <apex:outputLabel value="Refresh Time" for="refreshTime" />
                <apex:input type="time" value="{!refreshTime}" id="refreshTime"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:actionRegion>

    </apex:pageBlock>
</apex:form>

Here's some of my Apex Controller :

public class ConnectorHomeTabController {

public String selectedCadence {get; set;}
public String selectedWeekdayRefresh {get; set;}
public Time refreshTime {get; set;}
public Boolean isShowRefreshTime {get; set;}
public Boolean isShowRefreshWeekday {get; set;}

public Higi_ConnectorHomeTabController () {
    isShowRefreshTime    = false;
    isShowRefreshWeekday = false;
}

/**
 * Returns the select option list of cadence preferences 
 **/
public List<SelectOption> getCadenceOptions() {
    List<SelectOption> cadenceOptions = new List<SelectOption>();
    cadenceOptions.add(new selectOption('','No cadence specified'));
    cadenceOptions.add(new selectOption('Daily','Daily'));
    cadenceOptions.add(new selectOption('Manually','Manually'));
    cadenceOptions.add(new selectOption('Monthly','Monthly'));
    cadenceOptions.add(new selectOption('Realtime','Real-time'));
    cadenceOptions.add(new selectOption('Weekly','Weekly'));
    return cadenceOptions;
}

/**
 * Returns the select option list of weekdays 
 **/
public List<SelectOption> getWeekdayRefreshOptions() {
    List<SelectOption> weekdays = new List<SelectOption>();
    weekdays.add(new selectOption('','No weekday specified'));
    weekdays.add(new selectOption('Mon','Monday'));
    weekdays.add(new selectOption('Tue','Tuesday'));
    weekdays.add(new selectOption('Wed','Wednesday'));
    weekdays.add(new selectOption('Thu','Thursday'));
    weekdays.add(new selectOption('Fri','Friday'));
    return weekdays;
}


/**
 * Renders the fields depending on cadence picklist
 **/
public PageReference updateFieldVisibility() {
    if (selectedCadence == 'Monthly') {
        isShowRefreshWeekday = true;
        isShowRefreshTime    = true;
        System.debug('Cadence set to monthly');

    } else if (selectedCadence == 'Weekly') {
        isShowRefreshWeekday = true;
        isShowRefreshTime    = true;
        System.debug('Cadence set to weekly');

    } else if (selectedCadence == 'Daily') {
        isShowRefreshWeekday = false;
        isShowRefreshTime    = true;
        System.debug('Cadence set to daily');
    } else {
        isShowRefreshWeekday = false;
        isShowRefreshTime    = true;
        System.debug('Cadence is realtime or manual');
    }
    return ApexPages.currentPage();
}

}
3

Case Sensitive (in the few cases when it does matter) gets you every time:

<apex:actionSupport event="onChange" action="{!updateFieldVisibility}" reRender="editForm" />

should be

<apex:actionSupport event="onchange" action="{!updateFieldVisibility}" reRender="editForm" />

Also, per your comments, do not return the pagereference ApexPages.currentPage() as you are essentially reloading the page when you do that. Minimal Reproducible Example

Class

public class myExample{


    public boolean showValue{get;set;}
    public String selectedCadence {get; set;}



    public List<SelectOption> getCadenceOptions() {
        List<SelectOption> cadenceOptions = new List<SelectOption>();
        cadenceOptions.add(new selectOption('','No cadence specified'));
        cadenceOptions.add(new selectOption('Daily','Daily'));
        cadenceOptions.add(new selectOption('Manually','Manually'));
        cadenceOptions.add(new selectOption('Monthly','Monthly'));
        cadenceOptions.add(new selectOption('Realtime','Real-time'));
        cadenceOptions.add(new selectOption('Weekly','Weekly'));
        return cadenceOptions;
    }

    public void updateFieldVisibility() {
        showValue = true;
    }

}

VF Page

<apex:page controller="myExample">


<apex:form id="editForm">
    <apex:pageBlock title="Edit Organization Default Data Access" id="editPageBlock">


        <!-- Cadence specific fields -->
        <apex:actionRegion>
        <apex:pageBlockSection title="Cadence Settings" columns="2">

            <apex:pageBlockSectionItem>
                <apex:outputLabel value="Preferred Cadence" for="cadence" />
                <apex:selectList value="{!selectedCadence}" size="1" id="cadence" >

                    <apex:selectOptions value="{!cadenceOptions}" />
                    <apex:actionSupport event="onchange" action="{!updateFieldVisibility}" reRender="editForm" />
                </apex:selectList> 
            </apex:pageBlockSectionItem>

        </apex:pageBlockSection>

        <apex:pageBlockSection columns="1" id="additionalFields">
            <apex:pageBlockSectionItem rendered="{!showValue}">
                    I am being shown
            </apex:pageBlockSectionItem>

        </apex:pageBlockSection>
        </apex:actionRegion>

    </apex:pageBlock>
</apex:form>

</apex:page>

On Load enter image description here

On Select of value

enter image description here

When using onChange the text is not displayed.

  • I just tried this and unfortunately did not work /: – jalamak Sep 14 '16 at 23:54
  • @jalamak - Not sure how else to prove it than what was provided. It works just fine when you change it to "onchange" - Start with the code I have provided so you can prove to yourself that it does work, then add in your code until it breaks. Once you do you will find the next reason why it would not be showing. – Eric Sep 14 '16 at 23:56
  • If you want to prove the case matters, use exactly what I posted, prove to yourself it works, then change to "onChange" and watch it break – Eric Sep 14 '16 at 23:57
  • 1
    If you have not included all of your code for the VF page you may have required fields or something that is preventing the ajax request. You may want to put a page message component on your page to see any messages. Also check out the browser console for any errors. – Eric Sep 15 '16 at 0:06
  • The solution ended up being that I was returning a Page Reference instead of just having a void method that changes the boolean values. A lot of the examples I saw that followed this approach returned Page References as well, so I did the same. Thank you for your help! – jalamak Sep 15 '16 at 0:28

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.