Important points to remember regarding actionRegion, actionFunction, actionSupport, actionPoller

by Sudipta Deb | Jul 19, 2019 | actionFunction, actionPoller, actionRegion, actionSupport, Advanced Apex, Apex, Certification, javascript, PD II, visualforce | 2 comments

Last night(July 17th, 2019) while preparing for Platform Developer II certification, I was going through my notes to revise the different ways to call Apex Controller functions from Visualforce page, I thought of making a post regarding actionRegion, actionFunction, actionSupport, actionPoller & actionStatus. Just setting the expectation beforehand – It is not a post which will explain you all, but I will share the developer guide link for each of them so that you can go and read them (highly recommended).

actionRegion

The specifies the components to be processed on Force.com server. Without this tag, the whole page will be processed. With actionRegion also the whole form is submitted, but only the part within the tag will be processed.
It needs a reRender attribute to be effective. It will only refresh the part of the page using the data contained within the tag. This can be very effectively used to avoid validation errors that may occur due to present of standard validation rule in VF page like required attribute.
Here is the example without ActionRegion:
Controller:
public with sharing class WithoutActionRegionController {
public String valueEntered{get;set;}
public String country{get;set;}
public String state{get;set;}

public WithoutActionRegionController() {
country = '';
state = '';
valueEntered = '';
}

public PageReference countrySubmitted(){
valueEntered = 'Country: ' + country;
return null;
}
}
Visualforce Page:

<apex:page controller="WithoutActionRegionController">
<apex:form id="myForm">
<apex:pageMessages id="messages1"/>
<apex:pageBlock id="topPageBlock">
<apex:pageBlockSection columns="2" title="Country & State">
<apex:outputText value="Enter Country" />
<apex:inputText value="{!country}" >
<apex:actionSupport action="{!countrySubmitted}" reRender="messages1,outputId" event="onchange"/>
</apex:inputText>
<apex:outputText value="Enter State" />
<apex:inputText value="{!state}" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Output" id="outputId">
<apex:outputText value="{!valueEntered}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Output:

As you can see it throws the validation message due to state field is marked as required=true to moment I finish entering the country. This is because the whole page is submitted to render.

Now the same example with ActionRegion:

<apex:page controller="WithoutActionRegionController">
<apex:form id="myForm">
<apex:pageMessages id="messages1"/>
<apex:pageBlock id="topPageBlock">
<apex:pageBlockSection columns="2" title="Country & State">
<apex:outputText value="Enter Country" />
<apex:actionRegion>
<apex:inputText label="Enter Country" value="{!country}" >
<apex:actionSupport action="{!countrySubmitted}" reRender="messages1,outputId" event="onchange"/>
</apex:inputText>
</apex:actionRegion>
<apex:outputText value="Enter State" />
<apex:inputText value="{!state}" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Output" id="outputId">
<apex:outputText value="{!valueEntered}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Output:

This time no error message because actionRegion will render the Output and message1 part of the page with the content present inside tag. 

Reference: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionRegion.htm?search_text=actionregion

actionFunction

This allows calling controller functions directly from the JavaScript code using AJAX request. It is not the same as actionSupport which only allows calling controller functions from other Visualforce components. Few things to remember –

  • actionFunction is not allowed to be placed inside iteration component. It should be placed after the iteration component.
  • order is matched by the caller’s argument list. reRender attribute is needed to define
  • It doesn’t have event attribute. It doesn’t add Ajax request before calling the Controller method.
Example:
Controller:
public with sharing class ActionFunctionController {
public Boolean showHelloWorld{get;set;}
public String message {get;set;}
public ActionFunctionController(){
showHelloWorld = false;
message = '';
}

public PageReference shouldDisplayHelloWorld(){
showHelloWorld = true;
message = 'Now we can say Hello World';

return null;
}
}
Visualforce Page:
<apex:page controller="ActionFunctionController">
<apex:form>
<apex:actionFunction name="checkHelloWorldDisplay" action="{!shouldDisplayHelloWorld}" reRender="HelloWorldSection"/>
<apex:pageBlock>
<apex:pageBlockSection id="HelloWorldSection" columns="1">
<apex:inputCheckbox value="{!showHelloWorld}" onChange="checkHelloWorldDisplay()"/>
<apex:outputText value="{!message}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

actionSupport

This can be used when we need the Visualforce component to be refreshed asynchronously by the server when a particular event occurs like button click or mouseover.
  • It adds the AJAX request to the Visualforce Page and then calls the controller function. This is how it different from actionFunction.
  • It also has event attribute. Another difference between actionFunction and actionSupport.
  • It can be placed inside an iteration component.

actionPoller

  • A timer that sends an AJAX request to server according to a time interval specified. Each request can result full or partial refresh of the page.
  • Enabled attribute is used to make poller as active or inactive. Default is active.
  • It won’t time out due to inactivity.

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.

Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...

2 Comments

  1. Arup K Roy

    Nicely explained. 👍

    Reply
  2. Anonymous

    Thanks

    Reply

Leave a Reply