Created
November 10, 2015 23:59
-
-
Save brianmfear/440aa31ceea32e6c6755 to your computer and use it in GitHub Desktop.
salesforce.com apex:actionPoller example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActionPollerExample implements Database.Batchable<Integer>, Iterator<Integer>, Iterable<Integer> { | |
Integer counter; | |
public Id jobId { get; set; } | |
public Boolean keepPolling { get; set; } | |
public ActionPollerExample() { | |
counter = 0; | |
keepPolling = false; | |
} | |
public Iterator<Integer> iterator() { | |
return this; | |
} | |
public Integer next() { | |
return counter++; | |
} | |
public Boolean hasNext() { | |
return counter<20000; | |
} | |
public Iterable<Integer> start(Database.BatchableContext context) { | |
return this; | |
} | |
public void execute(Database.BatchableContext context, Integer[] scope) { | |
} | |
public void finish(Database.BatchableContext context) { | |
} | |
public void actionPollAction() { | |
AsyncApexJob job = [SELECT JobItemsProcessed, TotalJobItems, Status FROM AsyncApexJob WHERE Id = :jobId]; | |
if(job.Status == 'Queued' || job.Status == 'Holding' || job.Status == 'Preparing') { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'We are in the '+job.Status.toLowerCase()+' status...')); | |
} else if(job.Status != 'Completed' && job.Status != 'Aborted') { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'We are in '+job.Status.toLowerCase()+' status: '+Math.floor(100.0*job.JobItemsProcessed/job.TotalJobItems)+'%...')); | |
} else { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The job is now '+job.Status.toLowerCase()+'.')); | |
keepPolling = false; | |
} | |
} | |
public void startAction() { | |
jobId = Database.executeBatch(this); | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'We have started executing the job...')); | |
keepPolling = true; | |
} | |
public void showInitMessage() { | |
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'We will be starting the batch process in five seconds...')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="ActionPollerExample" action="{!showInitMessage}"> | |
<apex:form id="form"> | |
<apex:pageMessages /> | |
<apex:actionPoller rendered="{!jobId == null}" action="{!startAction}" interval="5" reRender="form" /> | |
<apex:actionPoller rendered="{!keepPolling}" action="{!actionPollAction}" interval="5" reRender="form" /> | |
</apex:form> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment