Tag Archives: Force.com

Action Support in Visualforce Page

Action support component adds AJAX support to other component. It allow the component to be refreshed asynchronously by calling the controller’s method when any event occurs (like onclik, onblur etc). It also allows to rerender page sections as desired.
In this article I will demonstrate how to use actionsupport in visualforce page.
Create Apex class with following code:

Public with sharing class TestActionSupport
{
    public Integer Total {get;set;}
     
    Public TestActionSupport()
    {
        Total = 0;
    }
     
    Public void IncreaseNumber()
    {
        Total ++;
    }
}

Now create the Visualforce page:

<apex:page controller="TestActionSupport">
<apex:form>
<apex:pageblock>
<apex:pageblocksection>
<apex:outputpanel id="idPanel1"> 
<apex:outputtext value="Click here to increase number">
<apex:actionsupport action="{!IncreaseNumber}" event="onclick" rerender="idResult">
</apex:actionsupport></apex:outputtext></apex:outputpanel>
<apex:outputtext id="idResult" label="No. of Clicked:" value="{!Total}">
</apex:outputtext></apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

In the above apex class, initially variable “Total” value is set to 0. But when we will click on visualforce page “Click here to increase number” then controller action method will be called and variable “Total” will be increase. The action support also rerender the outputText which refreshes the outputText and hence shows the new value set in controller method.

Before Click:
download

After Click:

download (1)

Difference between Custom Controller and Extension?

Custom Controller:

  • It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
  • You can use only one Apex class.
  • You can Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

Extension:

  • A controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.
  • It provides additional functionality to a controller – either a standard controller or a custom controller.
  • You can use multiple Apex classes separated by comma.
  • Use controller extensions when:You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.You want to add new actions.You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.

Difference between Standard Controller and Custom Controller?

Standard Controller:

  • Standard controller in apex, inherits all the standard object properties and standard button functionality directly.
  • It contain the same functionality and logic that are used for standard Salesforce pages.
  • It Can be used with standard objects and custom objects.
  • It can be extended to implement custom functionality using extensions keyword.
  • It provides a save method to allow you to persist changes.
  • You’d use this when you have a singe object to manipulate.

Custom Controller:

  • It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
  • Custom Controllers are associated with Visualforce pages through the controller attribute.
  • Custom controller defines its own functionality.
  • Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.
  • You’d use this when your page isn’t dealing with a main object.

Action Poller in Visualforce Page

Action poller acts as a timer in visualforce page. It sends an AJAX request to the server according to a time interval (time interval has to be specified or else it defaults to 60 seconds). Each request can result in a full or partial page update.
In this article I will demonstrate how to use actionpoller in visualforce page.

  • In the action attribute a controller method gets called. The method gets called with a frequency defined by the interval attribute which has to be greater than or equal to 5 seconds.
  • Time out can also be specified as an attribute in actionpoller. Once the time out point is reached it stops making AJAX callout to the server and controller method is no more called.

Create Apex class with following code:

Public with sharing class TestActionPoller
{
    Public  Integer Total{get;set;}
     
    Public TestActionPoller()
    {
        Total = 0;
    }
  
    Public void CountMethod()
    {
        Total++;
    }
}

Now create the Visualforce page:

<apex:page controller="TestActionPoller">
<apex:form>
<apex:outputtext id="idCount" value="Increase in every 5 seconds: {!Total}">
<apex:actionpoller action="{!CountMethod}" interval="5" rerender="idCount">
</apex:actionpoller>
</apex:outputtext></apex:form>
</apex:page>

In above visualforce page Action poller calls the method “CountMethod” every 5 seconds where the variable “Total” is updated. Rerender attribute refreshes the page block hence showing the updated value of variable “Total”.

download

Calling Visualforce Page From Javascript

Visualforce page that calls another Visualforce page upon confirming from javascript.

Visualforce Page:

<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="Open VF Page" onclick="OpenVFPage()"/>
        </apex:pageBlock>
    
    <script>
      function OpenVFPage(){
        var isConfirm = confirm('Do you want to open a new Visualforce Page?');
        if(isConfirm)
           window.open('/apex/YourVisualForcePage');
      }
     </script>
    </apex:form>
</apex:page>

Similarly, if you want to open a visualforce page from a custom button using javascript then use following piece of code.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var isConfirm = confirm('Do you want to open new Visualforce Page?');
if(isConfirm){
	window.parent.location.href="/apex/YourVisualforcePage";
}