We can display the progress of Batch Apex job on UI to user. Apex allows you to query the job status by query AsyncApexJob object and getting all details about running job like status, JobItemsProcessed, NumberOfErrors, TotalJobItems etc. By using apex, we can manipulate the result and display the progress bar for batch job.
Suppose you already have Apex Batch class written. Here for example "RecordTypeAccessFinder" is batch class is already present.
Below is VF page and Apex class Code:
Suppose you already have Apex Batch class written. Here for example "RecordTypeAccessFinder" is batch class is already present.
Below is VF page and Apex class Code:
This file contains hidden or 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 BatchJobProgressBarController { | |
private Id batchClassId; | |
final public String NOT_START = 'not_started'; | |
final public String PROCESSING = 'processing'; | |
final public String FINISHED = 'finished'; | |
public String batchStatus {get;set;} | |
public Id batchId {get;set;} | |
public String message {get;set;} | |
public Integer errornum {get;set;} | |
public BatchJobProgressBarController (){ | |
//here specify the Apex batch class name for which you want to display progress bar | |
batchClassId = [Select Name, Id From ApexClass Where Name = 'RecordTypeAccessFinder' Limit 1][0].id; | |
batchStatus = NOT_START; | |
batchId = null; | |
message = ''; | |
errornum =0; | |
} | |
public boolean getShowProgressBar() { | |
if(batchStatus == PROCESSING ) | |
return true; | |
return false; | |
} | |
//this method will fetch apex jobs and will update status,JobItemsProcessed,NumberOfErrors,TotalJobItems | |
public BatchJob[] getJobs() { | |
List<AsyncApexJob> apexJobs = | |
[Select TotalJobItems, Status, NumberOfErrors, ExtendedStatus, JobItemsProcessed, Id, JobType, ApexClassId, CreatedDate From AsyncApexJob Where ApexClassId =: batchClassId Order by CreatedDate DESC]; | |
if(apexJobs.size() == 0) { | |
return new List<BatchJob>(); | |
} | |
List<BatchJob> jobs = new List<BatchJob>(); | |
for(AsyncApexJob job : apexJobs) { | |
if(job.id != batchId) | |
continue; | |
BatchJob bj = new BatchJob(); | |
bj.isCompleted = false; | |
if(job.ApexClassId == batchClassId) { | |
bj.Job_Type = 'Process 1'; | |
} | |
bj.aj = job; | |
// NOT START YET | |
if(job.jobItemsProcessed == 0) { | |
bj.Percent= 0; | |
jobs.add(bj); | |
continue; | |
} | |
Decimal d = job.jobItemsProcessed; | |
d = d.divide(job.TotalJobItems, 2)*100; | |
bj.Percent= d.intValue(); | |
// PROCESSING | |
if(bj.Percent != 100){ | |
jobs.add(bj); | |
continue; | |
} | |
// FINISED | |
if(job.ApexClassId == batchClassId) { | |
batchStatus = FINISHED; | |
} | |
errornum += job.NumberOfErrors; | |
bj.isCompleted = true; | |
jobs.add(bj); | |
} | |
return jobs; | |
} | |
public PageReference StartBactJob() { | |
//execute RecordTypeAccessFinder batch | |
if(!test.isRunningTest()){ | |
string selectedObject='Account'; | |
RecordTypeAccessFinder acc=new RecordTypeAccessFinder(selectedObject); | |
batchId=database.executebatch(acc,10); | |
system.debug('************batchid:'+batchid); | |
} | |
batchStatus = PROCESSING; | |
return null; | |
} | |
public PageReference updateProgress() { | |
if(batchStatus == FINISHED) { | |
message = 'COMPLETED'; | |
} | |
return null; | |
} | |
public class BatchJob{ | |
public AsyncApexJob aj {get;set;} | |
public Integer Percent {get;set;} | |
public String Job_Type {get;set;} | |
public Boolean isCompleted {get;set;} | |
public BatchJob(){} | |
} | |
} |
This file contains hidden or 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="BatchJobProgressBarController"> | |
<style> | |
.prog-bar { | |
height: 14px; | |
margin: 3px; | |
padding: 0px; | |
padding-right:3px; | |
background: #FFA037; | |
text-align:right; | |
font-size:12px; | |
font-weight:bold; | |
color:#333333; | |
} | |
.prog-bar-done { | |
height: 14px; | |
margin: 3px; | |
padding: 0px; | |
padding-right:3px; | |
background: #C9DDEC; | |
} | |
.prog-border { | |
width: 209px; | |
background: #fff; | |
border: 1px solid silver; | |
margin: 0px; | |
padding: 0px; | |
} | |
</style> | |
<apex:form id="f1"> | |
<apex:pageBlock > | |
<apex:pageblockButtons location="top"> | |
<apex:commandButton value="Start batch Job" action="{!StartBactJob}" rerender="f1" status="actionstatus"/> | |
<apex:actionPoller rerender="f1" interval="5" action="{!updateProgress}" status="counterstatus"/> | |
</apex:pageblockButtons> | |
<apex:pageblockSection columns="1"> | |
<apex:pageBlockTable id="prog" value="{!Jobs}" var="item"> | |
<apex:column headerValue="{!$ObjectType.AsyncApexJob.fields.Status.label}" > | |
<apex:outputText value="{!item.aj.status}"/> | |
<apex:image url="/img/loading.gif" id="progressbar_img1" rendered="{!ShowProgressBar}" style="visibility: {!IF(item.isCompleted,'hidden','visible')}"/> | |
</apex:column> | |
<apex:column headerValue="Progress (%)"> | |
<div class="prog-border" > | |
<apex:outputPanel layout="block" styleClass="{!if(item.Percent>=100,'prog-bar-done','prog-bar')}" style="width: {!item.Percent*2}px;"> | |
<apex:outputText value="{!item.Percent}" rendered="{!if(item.Percent!=100,true,false)}" /> | |
</apex:outputPanel> | |
</div> | |
</apex:column> | |
</apex:pageBlockTable> | |
</apex:pageblockSection> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
Hi
ReplyDeleteQuite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming post to enhance my skill set, keep blogging.
Regards,
Cloud computing course in Chennai|cloud training in chennai
Thank you !! Amazing Write Up !!
ReplyDeleteBig data hadoop training in chennai
Web design training in chennai
Thank you !! Very usefull !!
ReplyDeletedot net training in chennai
dotnet projects chennai
Arduino training in chennai
The bar is set high for every one of the bloggers out there.
ReplyDeleteResumeyard
where is Batch class "RecordTypeAccessFinder" code
ReplyDeleteHi jyothi,
DeleteIt was assumption that you want to display progress bar for "RecordTypeAccessFinder". You can replace with any batch class that you have in your org and see the progress bar.
Hope this help!!
Thanks
Hello Sunil - Any tips to write a test class on this?
ReplyDeleteHello Sunil - I am trying to write a class for the controller. However I seem to have got stuck when I attempt to pass the batchid as its returning a null. Could you please help me know how do I go about this?
ReplyDeleteNice Post i learned a lot From the Post Thanks for sharing,learn the most ON-DEMAND software Training in Best Training Institutions
ReplyDeleteInstructor-LED Salesforce Online Training
Best Salesforce Online Training
Salesforce Training
ReplyDeleteIts Very a useful post to everyone and learn AWS from best IT training institute TO register Now
Amazon web Services Training
Salesforce training in Hitech city
Salesforce training in Hyderabad
Great share !!
ReplyDeleteieee final year projects in chennai
Robotics projects in chennai
Vlsi projects in chennai
Nice Article !!!
ReplyDeleteluxury homes in chennai
luxury properties for sale in chennai
buy luxury properties in chennai
You can awe your questioners by utilizing industry terms in your discussion amid the meeting, all since you took an online course. go check out
ReplyDeleteThey were searching for access to the business world and direction in how to approach this outside animal. more
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYou begin your job hunt by first identifying your transferable, functional, skills. In fact, you are identifying the basic building blocks of your work.job placement Consultants in India
ReplyDeleteYou can awe your questioners by utilizing industry terms in your discussion amid the meeting, all since you took an online course.
ReplyDeleteJob consultants hyderabad
Have you ever come across employments that did not have enough insights with respect to the job details or obligations? job posting
ReplyDeleteI love this blog!! The flash up the top is awesome!! job posting
ReplyDeleteI was looking at some of your posts on this website and I conceive this web site is really instructive! Keep putting up..
ReplyDeleteJobs in Dubai
Involving top management makes job seekers feel that the hire is an important position, and that they have personally been selected as the "candidate of choice" by the top brass. Contractors should begin closing the deal the moment they know that they want someone for hire. They should not let up until an offer is on the table and accepted.Cover letter
ReplyDeleteA career involves developing a long-term focus and workplace diversity Singapore viewing each job from a perspective of what has been learned and the skills that have been developed or acquired.
ReplyDeleteThree are usually cheap Ralph Lauren available for sale each and every time you wish to buy. anglia mea
ReplyDeleteYou are sharing a piece of nice information, it helped me. Keep it up and in the future sharing more articles like this. jobsearchine.com is here to help you to jobs in muncie Indiana.
ReplyDeleteThis article gives a piece of useful information. Keep updating.
ReplyDeleteaws career path
is aws certification worth it
blue prism latest version
rpa future scope
hadoop developer interview questions and answers for experienced
hadoop interview questions and answers for freshers
I got what you mean , thanks for posting .Woh I am happy to find this website through google. Fair Work Australia
ReplyDeleteWelcome to the party of my life here you will learn everything about me. Carpenter work jobs New York
ReplyDeleteYou make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. Voice of Nigeria
ReplyDeleteThe new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important. Unique Dofollow Backlinks
ReplyDeleteExcellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. 88카
ReplyDeleteIt is regular for job sheets with have great control norms to erase or alter up to 25% of all job postings as being unseemly. Most job sheets don't consider posting of messages, Url's, utilization of protected material, abusive comments, bogus, incorrect or misdirecting data, illicit or untrustworthy substance. https://europa-road.eu/hu/foldmunkagep-szallitas-debrecen.php
ReplyDeletePositive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Visa-Bisnis-India
ReplyDeleteSuperbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. 먹튀검증업체
ReplyDeleteI know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. 토토사이트
ReplyDeleteI am not sure where your getting your information, but good topic. I needs to spend some time learning much more or understanding more. 토토
ReplyDeleteThey were active listeners and 토토사이트viewed meetings deadlines as an imperative.
ReplyDeletehttps://maps.google.mu/url?q=https%3A%2F%2Fsite789.com
ReplyDeletesite849
ReplyDeleteThanks for giving so much of Information. Are you looking for Online teaching jobs from home in India. Apply for get the teaching jobs online from home. Become an online tutor in India
ReplyDeleteNice Blog... Thanks for sharing..
ReplyDeleteBest Project Center in Chennai