Find your content:

Search form

You are here

Want to show result after apex batch process is completed

 
Share

I need to show a alert message after the batch process is completed. For that i have create a static boolean varible in a class and in my batch process finish i am calling this varible and changing the value of the boolean varible, when the process is completed. I am using a apex button to run the batch process. The value of the boolean is get changed but i am not able to show the message when the process is completed. my batch file

 global void finish(Database.BatchableContext BC)
{
    System.debug('Batch Process Completed in finish 1');
    BatchUpdate_info.Updateresult = true;
    System.debug('Batch Process Completed in finish 2');
    system.debug('Batch process result ' + BatchUpdate_info.Updateresult);
}

} static var

public class BatchUpdate_info { 

public static boolean Updateresult = false; }

VF page to run the batch file

<apex:page controller="ctrl_ConatctBatch">
<apex:messages />
<apex:form >
<apex:commandButton value="Run Batch Process" action="{!getRunBatch}" />  
</apex:form>
</apex:page>

Controller

public class ctrl_ConatctBatch
{

public String getRunBatch() 
 {
     contact_BatchProcess cb = new contact_BatchProcess ();
     Database.executeBatch(cb, 200);
     System.debug('Result : In getRunBatch function');
     if(BatchUpdate_info.Updateresult == true){
        System.debug('Batch Process Completed');
        ApexPages.Message dupeMsg = new      ApexPages.Message(ApexPages.Severity.INFO,'Batch Process Completed');
        BatchUpdate_info.Updateresult = false;
        System.debug('Batch Update result ' + BatchUpdate_info.Updateresult);
     }

     return null;
 }

Attribution to: AnuRaj

Possible Suggestion/Solution #1

The better approach for this would be use an email alert to send to the User once a batch is completed .

If you want on the page an action poller or streaming API is the only way to detect this and show on to the page the moment batch completes .


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2


Update on 2016-01-19

The link previously shared was of un secure site so removed that, so here's briefly what to do:

  1. Get the Id of the Async Apex Job record: Id batchId = Database.executeBatch(myBatchClass);
  2. Query AsynxApexJob to get information about the Batch Process particularly the Status, Extended Status, Job Items Processed, Number of Errors, and Total Job Items. For more information about AsyncApexJob, see the DeveloperForce Workbench.
  3. Update your Visualforce Page or Lightning App with the queried AsyncApexJob data. There are many UIs available for Progress Bars particularly using HTML5. Essentially you want to show the % done as Job Items Processed / Total Job Items. And don't forget to have a way to show if the Batch failed (like turning the Progress Bar red or something -- maybe display the Status and Extended Status...). Links about Progress Bars: HTML5 Progress from CSS Tricks, Bootstrap 3 Progress Bars

Update on 2016-01-22

Found an answer of mine that has the code from the original link:

Best practices for monitoring Scheduled Apex and Batch Apex?


Attribution to: Scott Pelak
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5435

My Block Status

My Block Content