action support salesforce

Action support component adds AJAX support to another component, this allows the component to call a controller method when a particular event occurs(for example onlcik, onblur etc). It also allows to rerender,rendere page sections as desired.

In the following example a controller method is called when you click within a textbox using actionsuppot component.

Visualforce page

<apex:page controller="DemoController">
   <apex:form >
       <apex:pageBlock >
             Click Inside this block <apex:inputtext >
            <apex:actionSupport event="onclick" action="{!DemoMethod}" rerender="pgblck"/>
            </apex:inputtext>  
       </apex:pageBlock>
       <apex:pageblock id="pgblck">
             <apex:outputText value="{!outValueSecond }"/>
       </apex:pageblock>
   </apex:form>
</apex:page>

Controller

Public with sharing class DemoController {
Public String outValueSecond{get;set;}
Public String outvalue{get;set;}
Public boolean flag{get;set;}
  Public DemoController(){
     outvalue = 'Before Value';
     outValueSecond = 'before value set in constructor';
  }
   
  Public void DemoMethod(){
   outValueSecond = 'After value set in controller method. This method is called using action support added to inputtext compoennt';
  }
}

In this example initially the lower pageblock has a value that is set in constructor, but when the mouse is clicked in the text box the controller method is called which changes the value of the variable that is displayed in the lower pageblock. The controller is called by using action support for the inputtext. The action support also rerenders the lower page block which refreshes the lower block and hence shows the new value set in controller method.


















Action support for picklist in salesforce

One of the most common requirement that we come across, is building a picklist dependency in visualforce page. Wherein, we want to automatically set the dependent picklist based on controlling picklist field. For building such a functionality you can use action support as done in below example.

In this example, once the country is selected onchange of the picklist value action support get triggered and and it calls the method "capitalcal" which actually calculates the state value and then re renders the block that displays the state value calculated. Action support provides support for calling the controller class method on a ajax event and then also rerender the block or a section.

<apex:actionSupport> can be useful in building similar functionalities, you can also control other thing like images and rerender based on calculations done in a called class method. It helps in providing AJAX support to other visualforce components. Many time we have requirements where in we need to build dynamic picklists and also display values based on various calculations, at such time we can use actionsupport. Drop down values can be set using selectionoption and selectlist as done in below example. There are other attributes that can also be sued like status, timeout, rendered, etc.

Visualforce page

<apex:page controller="picklist_actionsupportcontroller" showHeader="true">
    <apex:form id="formId">
        <apex:pageBlock >
        <apex:pageBlockSection >
        <apex:selectList value="{!SelectedCountry}" size="1" >
            <apex:actionSupport event="onchange" action="{!capitalCal}" rerender="optxt"/>
            <apex:selectOptions value="{!Country}" />
        </apex:selectList>
        </apex:pageBlockSection> 
        <apex:pageblockSection >
        <apex:outputText id="optxt" value="{!CountryCapital}" label="Capital">           
        </apex:outputText>
        </apex:pageblockSection> 
        </apex:pageBlock>  
    </apex:form>
</apex:page>

Class

public with sharing class picklist_actionsupportcontroller {

 public SelectOption[] getCountry() {
        return new SelectOption[] { new SelectOption('-None-', '-None-'), 
            new SelectOption('India', 'India'), 
            new SelectOption('Japan', 'Japan'),
            new SelectOption('China', 'China')
        };
    }

    public String SelectedCountry{ get; set; }
    public string CountryCapital{get;set;}
    public PageReference capitalCal(){
        if(SelectedCountry == 'India')
        CountryCapital = 'Delhi';
        
        if(SelectedCountry == 'Japan')
        CountryCapital = 'Tokyo';
        
        if(SelectedCountry == 'China')
        CountryCapital = 'Beijing';
        
         
        if(SelectedCountry == 'New Zealand')
        CountryCapital = 'Auckland';
        return null;
    }

}

actionfunction and actionsupport in salesforce

i have already covered the difference between action function and action support in a separate post. <apex:actionFunction> is a visualforce component that provides support for calling a controller method directly from the visulaforce page java script. Where as, as disused above actionsupport provides support for other component in calling a controller method.

Action support vs action poller and action region

Action poller is a like a timer that can send ajax request on specified time intervals. Minimum time difference can be 5 sec. You can use action poller as counter, also make sure you don't overdo in a method that is being called using action poller and always keep it light with almost no dmls or query. The time interval with which you want to call the method should be specified in terms f second in a attribute "interval".


<apex:actionRegion> component is used to decide and segregate which component should be processes by server when an AJAX request is sent.

No comments:

Post a Comment