[SalesForce] ActionSupport with lookup field: is ActionRegion required

I have a VF page based on the Case object. I have the standard contact lookup field on this page. When the user enters a contact, I would like to retrieve immediately the contact mailing address fields and do some background calculation with the data before the form is submitted.

I know this works using a combination of ActionSupport and ActionRegion. See code below.

My question: is ActionRegion is required for this functionality? I only need the related contact data for background calculation, not for display. I want to avoid using ActionRegion because it forces the user to enter data in required fields in other parts of the page form.

However, when I remove ActionRegion, it looks like ActionSupport does not execute when I submit the VF page form. In the submit method, the related contact data is always null.

Is there a way to do this without using ActionRegion?

Here is the code pattern that I am using. I found this on Bob Buzzard's blog. http://bobbuzzard.blogspot.com/2011/11/retrieve-related-object-fields.html

    <apex:page standardcontroller="Contact" extensions="RelatedController">
 <apex:form >
   <apex:pageMessages id="msgs"/>
   <apex:pageBlock title="Contact Create/Edit">
      <apex:pageBlockSection title="Contact Information">
       <apex:inputField value="{!contact.FirstName}"/>
       <apex:inputField value="{!contact.LastName}"/>
      </apex:pageBlockSection>
       <apex:actionRegion >
          <apex:pageBlockSection id="accinfo" title="Account Information">
         <apex:inputField value="{!contact.AccountId}">
            <apex:actionSupport event="onchange" action="{!AccountPopulated}" rerender="accinfo, msgs"/> 
         </apex:inputField>
         <apex:outputField value="{!contact.Account.AccountNumber}"/>
         <apex:outputField value="{!contact.Account.Site}"/>
        </apex:pageBlockSection>
      </apex:actionRegion>
      <apex:pageBlockButtons >
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        <apex:commandButton value="Save" action="{!save}"/>
      </apex:pageBlockButtons>
   </apex:pageBlock>
  </apex:form>
</apex:page>

public with sharing class RelatedController 
{
    private ApexPages.StandardController stdCtrl;

    public RelatedController(ApexPages.StandardController std)
    {
        stdCtrl=std;
    }

    public void AccountPopulated()
    {
        Contact cont=(Contact) stdCtrl.getRecord();
        cont.Account=[select AccountNumber, Site from Account where id=:cont.AccountId];
    }
}

Best Answer

I want to avoid using ActionRegion because it forces the user to enter data in required fields in other parts of the page form.

One of the main uses of the apex:actionRegion tag is to do the opposite of this, by sending only the elements that are children of the actionRegion with the request to the server. The visualforce parser will only enforce required validations on fields that are included in the request, and thus you can strategically place an actionRegion to get around unwanted validation.

In this case of the code in your question, the apex:actionRegion tag is just wrapping the wrong elements. Change it so the actionRegion only wraps the inputField.

<apex:actionRegion>
     <apex:inputField value="{!contact.AccountId}">
        <apex:actionSupport event="onchange" action="{!AccountPopulated}" rerender="accinfo, msgs"/> 
     </apex:inputField>
</apex:actionRegion>
Related Topic