function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
michaelforcemichaelforce 

ActionSupport only works when Name field populated

I have a visualforce page with both actionSupport and some JavaScript interacting with an actionFunction.  The controller for the page is a standard controller (custom object) with one extension.  The goal of both the actionSupport and actionFunction is simply to rerender parts of the page.

 

The funny part is that the rerendering pieces will only work if the user has populated the name field... or if the URL had an Id in it, which means the record loaded in the standard controller already had a name.  Either way, the fancy rerenders simply don't happen unless the record the page is working with has a name.  I'm sure that behind the scenes there is a logical explanation... but I am baffled.

 

To recreate the scenario I will paste below a very simple page, class combo that uses the standard account controller.  Once you have created these in your org, navigate to the page and WITHOUT entering a name, change the "Industry" field a few times... nothing will happen.  Then go and give it a name, then change the Industry field again... magically, the rerender begins to work.  Any ideas?

 

Page:

 

<apex:page standardController="account" extensions="testWeirdBugExtension">
    <apex:form>
        <apex:pageBlock>
        
            <apex:pageBlockSection>
                <apex:inputField value="{!theRecord.Name}"/>
                <apex:pageblockSectionItem>
                    <apex:outputLabel>Industry</apex:outputLabel>
                    <apex:selectList value="{!theRecord.Industry}" multiselect="false" size="1">
                        <apex:selectOption itemValue="Retail" itemLabel="Retail"/>
                        <apex:selectOption itemValue="Manufacturing" itemLabel="Manufacturing"/>
                        <apex:actionSupport event="onchange" reRender="showOff"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
            
            <apex:outputPanel id="showOff">
                {!theRecord.Industry}
            </apex:outputPanel>

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

 

 

Extension class:

 

public class testWeirdBugExtension {

    public Account theRecord {get;set;}

    public testWeirdBugExtension(ApexPages.StandardController controller) {
        theRecord = (account)controller.getRecord();
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

This is because the Name field is required. If a required field is left blank no actions, and therefore no rerenders will occrur.There are two solutions.

 

1) Add required="false" attribute to name field.

 

2) Wrap the selectList in an actionRegion tag. This only sends the fields wrapped in this tag to the server for processing. It doesn't care if the Name is blank as it is not being sent for processing. See code below.

 

 

<apex:page standardController="account" extensions="testWeirdBugExtension">
    <apex:form>
        <apex:pageBlock>
        
            <apex:pageBlockSection>
                <apex:inputField value="{!theRecord.Name}"/>
                <apex:pageblockSectionItem>
                    <apex:outputLabel>Industry</apex:outputLabel>
                    <apex:actionRegion>
                        <apex:selectList value="{!theRecord.Industry}" multiselect="false" size="1">
                            <apex:selectOption itemValue="Retail" itemLabel="Retail"/>
                            <apex:selectOption itemValue="Manufacturing" itemLabel="Manufacturing"/>
                            <apex:actionSupport event="onchange" reRender="showOff"/>
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
            
            <apex:outputPanel id="showOff">
                {!theRecord.Industry}
            </apex:outputPanel>

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

 

- Jason

 

All Answers

TehNrdTehNrd

This is because the Name field is required. If a required field is left blank no actions, and therefore no rerenders will occrur.There are two solutions.

 

1) Add required="false" attribute to name field.

 

2) Wrap the selectList in an actionRegion tag. This only sends the fields wrapped in this tag to the server for processing. It doesn't care if the Name is blank as it is not being sent for processing. See code below.

 

 

<apex:page standardController="account" extensions="testWeirdBugExtension">
    <apex:form>
        <apex:pageBlock>
        
            <apex:pageBlockSection>
                <apex:inputField value="{!theRecord.Name}"/>
                <apex:pageblockSectionItem>
                    <apex:outputLabel>Industry</apex:outputLabel>
                    <apex:actionRegion>
                        <apex:selectList value="{!theRecord.Industry}" multiselect="false" size="1">
                            <apex:selectOption itemValue="Retail" itemLabel="Retail"/>
                            <apex:selectOption itemValue="Manufacturing" itemLabel="Manufacturing"/>
                            <apex:actionSupport event="onchange" reRender="showOff"/>
                        </apex:selectList>
                    </apex:actionRegion>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
            
            <apex:outputPanel id="showOff">
                {!theRecord.Industry}
            </apex:outputPanel>

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

 

- Jason

 

This was selected as the best answer
michaelforcemichaelforce

Thanks a lot man, actionRegion appears to have been created exactly for what I am trying to do.  My actual page is a bit more complicated then the example I posted, but I added some actionRegions where needed with 'renderRegionOnly' property set to false and 'immediate' set to true and it seems to be working now.

 

Cheers!