<apex:actionRegion> - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

<apex:actionRegion>

The ActionRegion tag is more useful that what I had thought for many years!

The documentation of this tag explains how this tag defines an "area of a Visualforce page that demarcates which components should be processed by the Force.com server when an AJAX request is generated." So I always thought "If you are asking a question to the user, why do you not listen to the full answer". You are asking for 10 fields, but you only want to listen to 5 answers... That does not make sense! Just ask for 5 fields.

But where I was missing the picture, is that this is not for the final answer, but for partial answers... Let me explain.

This form allows you to create opportunity records with 3 required fields, but when the stage field changes, the time at the bottom should be updated.



So I wrote this code...

<apex:page standardController="Opportunity">
    <apex:form id="thePageBlock" >
        <apex:pageBlock title="Edit Opportunity" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!quicksave}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!opportunity.name}" />
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.opportunity.fields.stageName.label}" for="stage"/>
                    <apex:inputField value="{!opportunity.stageName}" id="stage">
                        <apex:actionSupport event="onchange" rerender="thePageBlock" status="status"/>
                    </apex:inputField>
                </apex:pageBlockSectionItem>
                <apex:inputfield value="{!opportunity.closedate}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
        {!text(now())}
    </apex:form>
</apex:page>

The problem I was having is that in order to update the time, by using an actionSupport, I need to have the other fields validated. Wouldn't it be great if only the stage field would be processed? Well, that is exactly what the <apex:actionRegion> tag does for you. So I changed my code to this: 

<apex:page standardController="Opportunity">
    <apex:form id="thePageBlock" >
        <apex:pageBlock title="Edit Opportunity" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!quicksave}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!opportunity.name}" />
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.opportunity.fields.stageName.label}" for="stage"/>
                    <apex:actionRegion >
                        <apex:inputField value="{!opportunity.stageName}" id="stage">
                            <apex:actionSupport event="onchange" rerender="thePageBlock" status="status"/>
                        </apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                <apex:inputfield value="{!opportunity.closedate}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
        {!text(now())}: {!opportunity.stageName}
    </apex:form>
</apex:page>

It works like a charm!

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez