Apex Action Poller in Visualforce page salesforce

By | November 20, 2019

What is actionpoller?

When we want, the same action to be repeated we use actionpoller…

Actionpoller acts as a timer in visualforce page.

It is used to send an AJAX request to the server depending on the time interval that we specify in interval attribute (time interval has to be specified or else it defaults to 60 seconds).Each request can result in a full or partial page update.

<apex:actionPoller> has following important attributes:

  • interval: The time interval between AJAX update requests, in seconds. This value must be 5 seconds or greater, and if not specified, defaults to 60 seconds
  • action: The action method invoked by the periodic AJAX update request from the component.
  • reRender: Comma separated id’s of one or more components that are refreshed when request completes.

In the below example, actionpoller calls the method “callMethod” for every 10 seconds. In callMethod, the variable “seconds” counter is incremented by 10. Rerender attribute refreshes the outputText, so seconds value in page will be refreshed.

Apex Controller

public class actionpoller{
    public  Integer seconds{get;set;}
    
    public actionpoller(){
        seconds = 0;
    }
    
    public void callMethod(){
        seconds = seconds + 10;
    }    
}

Visualforce Page

<apex:page controller="actionpoller" tabStyle="Account">
    <apex:form >
        <apex:pageBlock id="myPageId">
            <apex:pageBlockSection title="actionPoller example" collapsible="false" columns="1">
              <apex:actionPoller action="{!callMethod}" reRender="out" interval="10"/>
              <apex:outputText value="{!seconds}" label="Time in seconds since action poller is called for every 10secs:" id="out"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
      </apex:form>
</apex:page>

Output

After 10sec the outPut will be changed as below screen shot

Leave a Reply

Your email address will not be published. Required fields are marked *