Tuesday, December 6, 2016

Visualforce - Beyond Basics Series 4 - actionpoller is your timer

How to perform timed action ?

if you need to fire action method calls (ajax calls to server) periodically, just like a timer, you need to use actionPoller tag. This will call the action methods at regular intervels & rerender the section of the screen(optionally).

Where is the code ?

The actionpoller tag can be illustration with following example. This updates the userRetries count everytime it sends the request to server. Finally, when it reached 10 times, it quits.
<apex:page controller="testcls4_06122016" >
<apex:form>
<apex:actionPoller interval="5" action="{!timerFired}"
enabled="{!userRetries < 10}" reRender="UserMessage" />
<apex:outputPanel id="UserMessage">
User Retry Count - {!userRetries}
</apex:outputPanel>
</apex:form>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub
public class testcls4_06122016 {
public Integer userRetries{get;set;}
public testcls4_06122016() {
userRetries = 0;
}
/* Action Methods */
public void timerFired() {
userRetries++;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


Precautions ?

you need to keep following things in mind, before using this tag

  •   Since, this calls repeatedly by itself & can run very silently in browser, it should be used only if it's required.
  •   One advantage of using this method is that it keeps the session alive & user login cannot timeout.
  •   The best practice is to avoid heavy DML, Call out, Trigger logic through it.



No comments:

Post a Comment

Thanks for reading my blog !