Skip to main content
added 520 characters in body
Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61

This code assumes there will be a custom setting storingan object the ID of the last jobjobs*

//Only showing the area that needs to change
global void finish(Database.BatchableContext BC) {
   string jobId = database.executeBatch(nextBatch);

   customSetting__c myCs = customSetting__c.getOrgDefault();

   if (myCs == null) {
    object__c myCsmyObject = new customSetting__cobject__c();
   }

   csmyObject.Id__cJobId__c = jobId;
   upsert cs;myObject;

}
<apex:page controller="myCont" action="{!scheduleJobgetJobs}">
    
<apex:form id="theForm">

<apex:pageMessages />

<apex:actionPollerrepeat action="value="{!checkCurrentJobtheJobs}" interval="5"var="job">
 reRender="theForm" <!-- Add all needed fields and make this pretty, maybe a table -->
    <apex:outputText value="{!job.Id}" /> -- <apex:outputText value="{!job.status}" />
  <br/>

</apex:repeat>

</apex:form>

</apex:page>
public without sharing myCont() { 
private string theId;

public voidList<AsyncApexJob> scheduleJob()theJobs {
            theId = customSetting__c.getOrgDefault().Id__cget;set;
}
public void checkCurrentJob() {
        if (String.isEmpty(theId)) { return; }
        
        List<AsyncApexJob> theJobs = [SELECT Id, Status FROM AsyncApexJob WHERE Id = :theId LIMIT 1];

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

            AsyncApexJobSet<String> jobjobIds = theJobs[0];

            //if it's cancelled or aborted, clear the record
            if (job.Status == 'Aborted'  || job.Status == 'Failed') {
                //Aborted
                ApexPages.addMessage(new ApexPages.MessageSet<String>(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
        For (object__c obj : if[select (job.StatusJobId__c ==from 'Completed'Object__c]) {
                //Done
                ApexPages.addMessage(new ApexPagesjobIds.Messageadd(ApexPages.Severityob.CONFIRM, 'Done')JobID__c);
            }
 
            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
              theJobs = ApexPages.addMessage(new[SELECT ApexPages.Message(ApexPages.Severity.INFOId, 'Info'));
            }

  Status FROM AsyncApexJob where Id in }:jobIds];
}

This code assumes there will be a custom setting storing the ID of the last job*

//Only showing the area that needs to change
global void finish(Database.BatchableContext BC) {
   string jobId = database.executeBatch(nextBatch);

   customSetting__c myCs = customSetting__c.getOrgDefault();

   if (myCs == null) {
     myCs = new customSetting__c();
   }

   cs.Id__c = jobId;
   upsert cs;

}
<apex:page controller="myCont" action="{!scheduleJob}">
    
<apex:form id="theForm">

<apex:pageMessages />

<apex:actionPoller action="{!checkCurrentJob}" interval="5" reRender="theForm"/>

</apex:form>

</apex:page>
public without sharing myCont() { 
private string theId;

public void scheduleJob() {
            theId = customSetting__c.getOrgDefault().Id__c
}
public void checkCurrentJob() {
        if (String.isEmpty(theId)) { return; }
        
        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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }
 
            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}

This code assumes there will be an object the ID of the jobs*

//Only showing the area that needs to change
global void finish(Database.BatchableContext BC) {
   string jobId = database.executeBatch(nextBatch);

   object__c myObject = new object__c();
   myObject.JobId__c = jobId;
   upsert myObject;

}
<apex:page controller="myCont" action="{!getJobs}">
    
<apex:form id="theForm">

<apex:pageMessages />

<apex:repeat value="{!theJobs}" var="job">
  <!-- Add all needed fields and make this pretty, maybe a table -->
    <apex:outputText value="{!job.Id}" /> -- <apex:outputText value="{!job.status}" />
  <br/>

</apex:repeat>

</apex:form>

</apex:page>
public without sharing myCont() { 
  public List<AsyncApexJob> theJobs {get;set;

public void getJobs() {
        Set<String> jobIds = new Set<String>();

        For (object__c obj : [select JobId__c from Object__c]) { 
          jobIds.add(ob.JobID__c);
         }
        

        theJobs = [SELECT Id, Status FROM AsyncApexJob where Id in :jobIds];
}
added 520 characters in body
Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61

This code assumes there will be a custom setting storing the ID of the last job*

Batchable Class:

//Only showing the area that needs to change
global void finish(Database.BatchableContext BC) {
   string jobId = database.executeBatch(nextBatch);

   customSetting__c myCs = customSetting__c.getOrgDefault();

   if (myCs == null) {
     myCs = new customSetting__c();
   }

   cs.Id__c = jobId;
   upsert cs;

}

Page:

public without sharing myCont() { 
private string theId;

public void scheduleJob() {
            string theId = DatabasecustomSetting__c.executeBatchgetOrgDefault(MyBatchClass, 200);.Id__c
}
public void checkCurrentJob() {
        if (String.isEmpty(theId)) { return; }
        
        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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}

Page:

public without sharing myCont() { 
private string theId;

public void scheduleJob() {
            string theId = Database.executeBatch(MyBatchClass, 200);
}
public void checkCurrentJob() {
        
        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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}

This code assumes there will be a custom setting storing the ID of the last job*

Batchable Class:

//Only showing the area that needs to change
global void finish(Database.BatchableContext BC) {
   string jobId = database.executeBatch(nextBatch);

   customSetting__c myCs = customSetting__c.getOrgDefault();

   if (myCs == null) {
     myCs = new customSetting__c();
   }

   cs.Id__c = jobId;
   upsert cs;

}

Page:

public without sharing myCont() { 
private string theId;

public void scheduleJob() {
            theId = customSetting__c.getOrgDefault().Id__c
}
public void checkCurrentJob() {
        if (String.isEmpty(theId)) { return; }
        
        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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}
added 722 characters in body
Source Link
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

Page:

<apex:page controller="myCont" action="{!scheduleJob}">
    
<apex:form id="theForm">

<apex:pageMessages string/>

<apex:actionPoller theIdaction="{!checkCurrentJob}" =interval="5" Database.executeBatch(MyBatchClass,reRender="theForm"/>

</apex:form>

</apex:page>

Class:

public 200without sharing myCont(); { 
private string theId;

public void scheduleJob() {
     //You can run the code below elsewhere.. string theId = Database.executeBatch(MyBatchClass, maybe200);
}
public viavoid ancheckCurrentJob() 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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}

}

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
            }

        }

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

Here is a snippet

Page:

<apex:page controller="myCont" action="{!scheduleJob}">
    
<apex:form id="theForm">

<apex:pageMessages />

<apex:actionPoller action="{!checkCurrentJob}" interval="5" reRender="theForm"/>

</apex:form>

</apex:page>

Class:

public without sharing myCont() { 
private string theId;

public void scheduleJob() {
            string theId = Database.executeBatch(MyBatchClass, 200);
}
public void checkCurrentJob() {
        
        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
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'Aborted'));
            }

            //if it is completed
            if (job.Status == 'Completed') {
                //Done
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Done'));
            }

            //If it is alive, show progress
            if (job.Status == 'Holding' || job.Status == 'Queued' || job.Status == 'Preparing' || job.Status == 'Processing') {
                //In Progress
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Info'));
            }

        }
}

}

Source Link
Sebastian Kessel
  • 13.2k
  • 12
  • 38
  • 61
Loading