Skip to main content
1 of 4
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61

Store the Id of the job you ran, and then you can query the AsyncApexJob table to see progress.

Here is a snippet

        string theId = Database.executeBatch(MyBatchClass, 200);

        //You can run the code below elsewhere... maybe via an actionPoller in a page
        List<AsyncApexJob> theJobs = [SELECT Id, Status FROM AsyncApexJob WHERE Id = :theId LIMIT 1];

        if (theJobs.isEmpty()) {    
            //Oops, I have the wrong one
        } else {

            AsyncApexJob job = theJobs[0];

            //if it's cancelled or aborted, clear the record
            if (job.Status == 'Aborted'  || job.Status == 'Failed') {
                //Aborted
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
            }

        }
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61