I have one vf page which contains one custom button and two pageblocktable. On click of button my batch class will run and 1st pageblocktable will show status of batch class which is currently running and the second pageblocktable will show the the records which are processed in the batch. As of now my 2nd pageblocktable is also showing previous records which got processed in batch class. Also I need to rerender the 2nd pageblocktable once the status of my batch class is complete.
Page:-
<!-- Here is the css styles that will be used for the progress bars -->
<style>
.progressBar{
background-color: #f8f8f8;
border:1px solid #DDDDDD;
height: 19px;
width: 450px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
.progress{
background-color: #F7B64B;
border:1px solid #E78F08;
height: 100%;
margin: -1px;
text-align: center;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
line-height: 18px;
}
</style>
<script type="text/javascript">
check(){
var variable12 = true;
}
</script>
<apex:form >
<apex:commandButton value="Start Batch Job" action="{!startBatch}" reRender="jobs,error"/>
<apex:outputText id="error" value="{!error}" style="font-weight: bold; color: red"/>
<!-- This action poller will check the status of the batch jobs every 5 seconds -->
<apex:actionPoller rerender="jobs" interval="5" />
<apex:pageBlock id="jobs" title="Batch Apex Jobs" rendered="{!variable12}">
<apex:pageBlockTable value="{!CustomController}" var="b">
<apex:column value="{!b.job.ApexClass.Name}"/>
<apex:column value="{!b.job.CreatedDate}"/>
<!-- <apex:column value="{!b.job.CreatedById}"/> -->
<apex:column value="{!b.job.Status}"/>
<apex:column width="500px" >
<div class="progressBar">
<div class="progress" style="width: {!b.percentComplete}%;">
{!b.percentComplete}%
</div>
</div>
</apex:column>
<apex:column value="{!b.job.CompletedDate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Assignment Results" id="ar">
<apex:pageblocktable value="{!recordsProcessed}" var="rp" id="recProcess">
<apex:column value="{!rp.Order.OrderNumber}"/>
<apex:column value="{!rp.Order.Status}"/>
<apex:column value="{!rp.OrderItemNumber}"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
Controller:-
public with sharing class CustomController {
public String getBoolValue() {
return null;
}
public boolean variable12{get{return true;}set;}
public CustomController()
{
system.debug('Inside constructor... ');
}
boolean boolValue=false;
MyBatchClass ordDiscObj = new MyBatchClass();
public String error;
Id batchprocessid;
DateTime dateTimeObj;
public PageReference startBatch() {
variable12=true;
List<AsyncApexJob> openJobs = [SELECT Id from AsyncApexJob where Status = 'Processing' OR Status = 'Queued'];
if(openJobs.size() < 5){
batchprocessid = Database.executeBatch(ordDiscObj);
}else{
error = 'Only five batch jobs at a time.';
}
return null;
}
public List<OrderItem> getRecordsProcessed() {
//AsyncApexJob getCreatedDate = [SELECT Id,CreatedDate from AsyncApexJob WHERE Id =:batchprocessid];
//MyBatchClass disOBj = new MyBatchClass();
//system.debug('Recordssss insideeee controllerrrr.... '+disOBj.listForVF);
List<OrderItem> ordList = new List<OrderItem>();
ordList = [SELECT Id,Order.Status,Order.OrderNumber,OrderItemNumber FROM OrderItem WHERE IsProcessedInBatch__c= true AND LastModifiedDate >: dateTimeObj LIMIT 1000];
return ordList;
}
public String getError() {
return null;
}
public List<BatchJob> CustomController;
public Integer numberOfJobs {get;set;}
public List<BatchJob> getCustomController(){
CustomController= new List<BatchJob>();
for(AsyncApexJob a : [select TotalJobItems, Status, NumberOfErrors, MethodName, JobType, JobItemsProcessed, Id, CreatedDate, CreatedById, CompletedDate, ApexClassId, ApexClass.Name From AsyncApexJob where Id =:batchprocessid ]){
Double itemsProcessed = a.JobItemsProcessed;
Double totalItems = a.TotalJobItems;
dateTimeObj = a.CreatedDate;
BatchJob batchObj = new BatchJob();
batchObj.job = a;
if(totalItems == 0){
batchObj.percentComplete = 0;
}else{
batchObj.percentComplete = ((itemsProcessed / totalItems) * 100.0).intValue();
system.debug('Percentageeeee.... '+batchObj.percentComplete);
system.debug('Check in controllerrr... '+StaticClass.checkIfClassFinished1);
}
CustomController.add(batchObj);
}
return CustomController;
}
public Class BatchJob{
public AsyncApexJob job {get;set;}
public Integer percentComplete {get;set;}
}
}